How do you find the top-N per group (e.g., top 3 products per category by revenue) using SQL?

Quick Overview

Asks how to return the top N rows within each group in SQL, such as the three highest-revenue products in every category. Tests how you structure the query, how ties at the cutoff are handled, and when other query shapes perform better on large tables.

How do you find the top-N per group (e.g., top 3 products per category by revenue) using SQL?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

How do you find the top-N per group (for example, the top 3 products per category by revenue) using SQL? Explain the query pattern, how it treats ties, and what alternatives exist. ```hint Per-row values within a group Look for a SQL feature that computes a value for each row from the other rows in its group without collapsing the rows. ``` ```hint Evaluation order Think about when SQL evaluates the WHERE clause compared with the expressions in the SELECT list. ``` ### Clarifying Questions to Ask - Is revenue already stored as one row per product, or does it need to be summed from order lines first? - If two products tie for third place, should both appear, or exactly three rows per category? - What should happen to categories with fewer than three products, and to products with no revenue recorded? - Which database is this for, and roughly how many rows and categories are there? ### What a Strong Answer Covers - The core query pattern for ranking rows within each group. - Where the rank filter is applied and why, including dialect-specific options. - How the ranking choice handles ties at the cutoff, and how to match it to the business rule. - Aggregating to one row per product before ranking, and making the output reproducible. - Alternative query shapes and when each one performs well. ### Follow-up Questions - Two products tie for third place in a category. How would you return both, and how would you return exactly one in a reproducible way? - The sales table has hundreds of millions of rows and a few thousand categories. Which approach and which index would you choose? - How would you return the top 3 products per category for each month? - How would you add each product's share of its category's total revenue to the same result?

Overview: Asks how to return the top N rows within each group in SQL, such as the three highest-revenue products in every category. Tests how you structure the query, how ties at the cutoff are handled, and when other query shapes perform better on large tables.

General logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

How do you find the top-N per group (for example, the top 3 products per category by revenue) using SQL? Explain the query pattern, how it treats ties, and what alternatives exist.

Clarifying Questions to Ask Guidance

  • Is revenue already stored as one row per product, or does it need to be summed from order lines first?
  • If two products tie for third place, should both appear, or exactly three rows per category?
  • What should happen to categories with fewer than three products, and to products with no revenue recorded?
  • Which database is this for, and roughly how many rows and categories are there?

What a Strong Answer Covers Guidance

  • The core query pattern for ranking rows within each group.
  • Where the rank filter is applied and why, including dialect-specific options.
  • How the ranking choice handles ties at the cutoff, and how to match it to the business rule.
  • Aggregating to one row per product before ranking, and making the output reproducible.
  • Alternative query shapes and when each one performs well.

Follow-up Questions Guidance

  • Two products tie for third place in a category. How would you return both, and how would you return exactly one in a reproducible way?
  • The sales table has hundreds of millions of rows and a few thousand categories. Which approach and which index would you choose?
  • How would you return the top 3 products per category for each month?
  • How would you add each product's share of its category's total revenue to the same result?