IntermediateWindow functions

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
SQL
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

SQL
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;
Result
categoryby_row_numberby_rankby_dense_rank
Books345
Games333

Top 3 per category, ties included (RANK)

SQL
SELECT category, product, revenue
FROM product_revenue
QUALIFY RANK() OVER (PARTITION BY category ORDER BY revenue DESC) <= 3
ORDER BY category, revenue DESC, product;
Result
categoryproductrevenue
BooksSQL Basics900
BooksSpark Guide900
BooksData Design700
BooksKafka Notes700
GamesChess800
GamesGo600
GamesPoker300

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_NUMBER tie-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 <= 3 means at most 3 rows.
  • Leaving the tie-breaker out of ROW_NUMBER, so different runs show different products.

More on this topic in the SQL Interview Questions guide, or browse all SQL questions.