▸_sqlgym

59. Each Customer's Most Expensive Order

HardSubqueriesgreatest-n-per-groupcorrelated subqueryMAX

Greatest-n-per-group asks for the top row within each group. A classic approach keeps a row only when no other row in its group beats it — expressed with a correlated subquery.

Task: return each customer's single most expensive order. The data is built so each customer's maximum amount is unique (no ties), giving exactly one row per customer.

Return columns customer, order_id, amount. Order by customer ascending.

Tables
Loading schema…
Expected outputrows must come back in this order
Computing expected output…

This is the result for the example data above. On Submit your query is graded against this example plus 4 hidden edge cases 5test cases in all. A sloppy query that only fits the example won't pass.

SQL
⌘/Ctrl+↵ run · ⇧⌘/Ctrl+↵ submit
Run a query to see its output here.