Top 3 products by revenue per category: ROW_NUMBER, RANK or DENSE_RANK?
Scenario
Merchandising wants the top 3 products in each category for a homepage banner. In Books, two products share first place and two share third.
Interview answerTry answering first, then reveal
It depends on how ties should be handled, so ask. ROW_NUMBER <= 3 returns exactly 3 rows and drops one tied product (pick it with a tie-breaker). RANK <= 3 returns every product tied for a top-3 place, so it can return more than 3. DENSE_RANK <= 3 returns the products with the top 3 revenue values, which can be many more rows.
Explanation
A banner with exactly three slots needs ROW_NUMBER plus a deliberate tie-breaker (for example newest product or best rating).
A "top performers" report usually wants RANK: anyone tied with third place is included, which is the fair reading of "top 3".
DENSE_RANK <= 3 answers a different question: "which products have one of the three highest revenue figures?". With many ties it can return many products.
Worked example
Run on DuckDB when this page was built; the results shown are the real output.
Sample data
CREATE TABLE product_revenue (category VARCHAR, product VARCHAR, revenue INT);
INSERT INTO product_revenue VALUES
('Books', 'SQL Basics', 900), ('Books', 'Spark Guide', 900), ('Books', 'Data Design', 700),
('Books', 'Kafka Notes', 700), ('Books', 'dbt Primer', 600), ('Books', 'Iceberg Intro', 400),
('Games', 'Chess', 800), ('Games', 'Go', 600), ('Games', 'Poker', 300), ('Games', 'Bridge', 200);How many rows each rule returns per category
WITH ranked AS (
SELECT category, product, revenue,
ROW_NUMBER() OVER (PARTITION BY category ORDER BY revenue DESC, product) AS rn,
RANK() OVER (PARTITION BY category ORDER BY revenue DESC) AS rnk,
DENSE_RANK() OVER (PARTITION BY category ORDER BY revenue DESC) AS drnk
FROM product_revenue
)
SELECT category,
COUNT(*) FILTER (WHERE rn <= 3) AS by_row_number,
COUNT(*) FILTER (WHERE rnk <= 3) AS by_rank,
COUNT(*) FILTER (WHERE drnk <= 3) AS by_dense_rank
FROM ranked
GROUP BY category
ORDER BY category;| category | by_row_number | by_rank | by_dense_rank |
|---|---|---|---|
| Books | 3 | 4 | 5 |
| Games | 3 | 3 | 3 |
Top 3 per category, ties included (RANK)
SELECT category, product, revenue
FROM product_revenue
QUALIFY RANK() OVER (PARTITION BY category ORDER BY revenue DESC) <= 3
ORDER BY category, revenue DESC, product;| category | product | revenue |
|---|---|---|
| Books | SQL Basics | 900 |
| Books | Spark Guide | 900 |
| Books | Data Design | 700 |
| Books | Kafka Notes | 700 |
| Games | Chess | 800 |
| Games | Go | 600 |
| Games | Poker | 300 |
In Books, RANK numbers the products 1, 1, 3, 3, 5, 6 and DENSE_RANK numbers them 1, 1, 2, 2, 3, 4, so the three rules return 3, 4 and 5 products. Games has no ties, so all three agree.
In production
- Write the tie rule into the specification or the column comment. "Top 3" is ambiguous and the three functions give different answers.
- If the result feeds a UI with a fixed number of slots, a deterministic
ROW_NUMBERtie-breaker stops the banner changing on every refresh.
Common mistakes
- Using
LIMIT 3: that gives the top 3 overall, not per category. - Assuming
DENSE_RANK <= 3means at most 3 rows. - Leaving the tie-breaker out of
ROW_NUMBER, so different runs show different products.
Related questions
More on this topic in the SQL Interview Questions guide, or browse all SQL questions.