Stop 08 · Window FunctionsProjects · 1/2

Top-N Products per Category Report

Part of the SQL Roadmap

In short

In this project you build a report every category manager at an online store would want on their desk: the top 3 products by revenue in each category last quarter, with each one's share of the category.

A project that ranks products within each category by revenue and returns the top 3 per category, a classic analyst and interview task.

The jaadu stacks three favourite products on a bright display shelf at the end of each supermarket aisle while shoppers push trolleys past.

Think of it like this

The supermarket end-cap shelves

Walk into a big supermarket in Bengaluru and every aisle ends with a special shelf showing that aisle's bestsellers. The snacks aisle shows its top snacks, the dairy aisle its top dairy. Your report is the plan for those end-cap shelves, built from real sales.

Watch it happen

From categories to the top 3 in each

All products
Electronics
Home & Kitchen
#1 Earbuds ₹4.1L
#2 Power bank ₹3.2L
#2 Smartwatch ₹3.2L
#3 Cable ₹0.9L
#1 Pressure cooker ₹5.6L
#2 Mixer grinder ₹4.4L
#3 Steel tiffin ₹1.8L

1/5Start from every product sold last quarter, with revenue summed per product.

The big ideas

What you need to know

Fix the time window

Decide the period up front, such as the last full quarter, and filter on it before ranking.

State your tie rule

The brief says DENSE_RANK, so tied products both show. Write one line in the report explaining this.

Add share of category

Product revenue divided by SUM(revenue) OVER (PARTITION BY category) shows how dominant each bestseller is.

Sanity check totals

Add up revenue per category from your CTE and compare with a plain GROUP BY. They should match.

How it works

Building the report

  1. 1

    Join and filter

    Join order_items to products and keep only last quarter's orders.

  2. 2

    Sum per product

    GROUP BY category and product to get revenue per product.

  3. 3

    Rank inside category

    DENSE_RANK by revenue, partitioned by category, and add each product's share.

  4. 4

    Keep the top

    Filter rank <= 3 in an outer query and sort by category and rank.

See it in code

Walk through the code

sql
1WITH product_revenue AS (2  SELECT3    p.category,4    p.product_name,5    SUM(oi.quantity * oi.unit_price) AS revenue6  FROM order_items oi7  JOIN orders o   ON o.order_id = oi.order_id8  JOIN products p ON p.product_id = oi.product_id9  WHERE o.order_date >= DATE '2026-07-01'10    AND o.order_date <  DATE '2026-10-01'11  GROUP BY p.category, p.product_name12),13ranked AS (14  SELECT15    category,16    product_name,17    revenue,18    DENSE_RANK() OVER (PARTITION BY category ORDER BY revenue DESC) AS rnk,19    ROUND(100.0 * revenue / SUM(revenue) OVER (PARTITION BY category), 1) AS pct_of_category20  FROM product_revenue21)22SELECT category, rnk, product_name, revenue, pct_of_category23FROM ranked24WHERE rnk <= 325ORDER BY category, rnk, product_name;

1/5Revenue per product comes from order lines joined to orders and products.

The heart of the report: Q3 revenue per product, ranked within its category, with its share. Runs as is in PostgreSQL. MySQL 8+ and SQL Server accept it if you write the dates as plain strings like '2026-07-01'.

Save yourself the pain

Common mistakes

Oops

Filtering rnk <= 3 before computing pct_of_category.

Do this instead

Compute the share in the same CTE as the rank, so the denominator is the whole category, not just its top 3.

Oops

Using BETWEEN '2026-07-01' AND '2026-09-30' on a timestamp column.

Do this instead

That misses everything after midnight on 30 September. Use >= start and < next day.

Your mission

Ship the report

~60 min

Quick check

Why compute pct_of_category in the same CTE as the rank instead of after WHERE rnk <= 3?

Go deeper

  1. 1Use DENSE_RANK with PARTITION BY category and filter in an outer query.
  2. 2Add each product's share of its category's revenue.
  3. 3Explain in a short note how you handled ties.

More projects at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge