▸_sqlgym

10. Top 2 Products per Category

MediumRankingROW_NUMBERPARTITION BYtop-N-per-group

The "top N per group" pattern: rank rows within each group, then keep the ones at or below N.

Task: return the two highest-revenue products in each category. Columns: category, product, revenue. Order by category, then revenue descending.

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.