GPT-5.6 Sol
10.0# The query fully satisfies the requirements with correct aggregation, deterministic top-customer ranking, window-based revenue percentage, appropriate rounding, clear structure, and comments for every CTE.
inspect full trace →Write a complex SQL query with CTEs, window functions, and aggregations
Write a single SQL query (PostgreSQL dialect) against the following schema: ```sql CREATE TABLE orders ( id SERIAL PRIMARY KEY, customer_id INTEGER NOT NULL, product_id INTEGER NOT NULL, amount NUMERIC(10,2) NOT NULL, created_at TIMESTAMPTZ NOT NULL ); CREATE TABLE customers ( id SERIAL PRIMARY KEY, name TEXT NOT NULL, country TEXT NOT NULL ); ``` The query must return, for each country: 1. `country` — the country name 2. `total_revenue` — sum of all order amounts 3. `order_count` — total number of orders 4. `avg_order_value` — average order amount, rounded to 2 decimal places 5. `top_customer` — the name of the customer with the highest total spend in that country (break ties by customer name ascending) 6. `revenue_pct` — this country's share of global revenue, as a percentage rounded to 1 decimal place Requirements: - Use at least one CTE - Use a window function for `revenue_pct` - Order results by `total_revenue` descending - Add a comment above each CTE explaining what it computes
Is revenue per country summed correctly via JOIN + GROUP BY? Is `top_customer` correctly found using DISTINCT ON, ROW_NUMBER(), or a correlated subquery? Is `revenue_pct` computed with a window function (SUM(...) OVER ()) rather than a scalar subquery? Is rounding applied correctly (2dp for avg, 1dp for pct)?
Is the query readable — CTEs named clearly, columns aliased cleanly? Is there a single final SELECT rather than redundant nesting? Are aggregate functions used correctly?
Does each CTE have a comment explaining what it computes? Are column aliases self-explanatory? Is the overall query structure easy to follow top-to-bottom?
# The query fully satisfies the requirements with correct aggregation, deterministic top-customer ranking, window-based revenue percentage, appropriate rounding, clear structure, and comments for every CTE.
inspect full trace →# The query correctly aggregates revenue and orders, ranks top customers with deterministic tie-breaking, uses a window function for global revenue share, and documents every CTE; the only minor ambiguity is that its LEFT JOIN includes countries and top customers with no orders.
inspect full trace →