Business Intelligence Path · Mission 16 of 25Hard

Top 2 products per category

Use ROW_NUMBER() with PARTITION BY to solve top-N per group problems

Back to Business Intelligence

The Brief

Priya NairHead of Productproduct-analytics

For the quarterly review, I need the top 2 products by revenue in each category (normalized). Show the category, product name, total revenue, and the rank within its category. Only the top 2 per category — no more.

You'll practice

ROW_NUMBER()PARTITION BYTop-N

Tables available

fact_salesdim_products

Hints (3)

Each hint you reveal reduces the XP you can earn. Try the query first.

Hint 1

Use ROW_NUMBER() OVER (PARTITION BY LOWER(category) ORDER BY total_revenue DESC) to rank within each category

Hint 2

You need to aggregate revenue per product first in a subquery, then apply the window function, then filter to rank <= 2 in an outer query

Hint 3

Three layers: innermost aggregates, middle applies ROW_NUMBER, outermost filters WHERE rn <= 2