The Podium Finish
A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.
- Domain
- SQL
- Difficulty
- medium
- Seniority
- L4
- Asked in
- Meta
Problem
For each category, return the top 2 products by total quantity sold, along with each product's position within its category. When products tie on quantity, the alphabetically earlier name takes the higher position, and no 2 products ever share one.
Every category gets a podium with room for only 2.
Tables
transactions
Columns: transaction_id INTEGER, user_id INTEGER, product_id INTEGER, quantity INTEGER, total_amount REAL, transaction_date DATE
| transaction_id | user_id | product_id | quantity | total_amount | transaction_date |
|---|---|---|---|---|---|
| 1067 | 197 | 1001 | 2 | 23.46 | 2026-02-02 |
| 1134 | 294 | 1050 | 3 | 36.93 | 2026-03-03 |
| 1201 | 391 | 1099 | 4 | 50.4 | 2026-04-04 |
| 1268 | 488 | 1148 | 5 | 63.87 | 2026-05-05 |
| 1335 | 585 | 1197 | 0 | 0 | 2026-06-06 |
| 1402 | 682 | 1246 | 2 | 90.81 | 2026-07-07 |
| 1469 | 779 | 1295 | 3 | 104.28 | 2026-08-08 |
| 1536 | 876 | 1344 | 4 | 117.75 | 2026-09-09 |
| 1603 | 973 | 1393 | 5 | -45 | 2026-01-10 |
| 1670 | 1070 | 1442 | 1 | 144.69 | 2026-02-11 |
products
Columns: product_id INTEGER, product_name TEXT, category TEXT, price REAL, rating REAL, in_stock BOOLEAN
| product_id | product_name | category | price | rating | in_stock |
|---|---|---|---|---|---|
| 1001 | Basic Device 1X | Books | 17.52 | 2.7 | 1 |
| 1050 | Deluxe Bundle 2X | Clothing | 25.05 | 4.4 | 1 |
| 1099 | Pro Unit 3X | Home & Kitchen | 32.58 | 2.1 | 1 |
| 1148 | Ultra Tool 4X | Collectibles | 40.11 | 3.8 | 1 |
| 1197 | Essential Set 5X | Toys | 47.64 | 1.5 | 1 |
| 1246 | Classic System 6X | Beauty | 0 | 3.2 | 1 |
| 1295 | Smart Gadget 7X | Automotive | 62.7 | 4.9 | 0 |
| 1344 | Eco Kit 8X | Garden | 70.23 | 2.6 | 1 |
| 1393 | Turbo Pack 9X | Music | 77.76 | 4.3 | 0 |
| 1442 | Premium Widget 10X | Electronics | 85.29 | 2 | 1 |
Expected output
| category | product_name | total_quantity | category_rank |
|---|---|---|---|
| Automotive | Smart Gadget 27X | 6 | 1 |
| Automotive | Smart Gadget 37X | 6 | 2 |
| Beauty | Classic System 26X | 4 | 1 |
| Beauty | Classic System 36X | 4 | 2 |
| Books | Basic Device 1X | 6 | 1 |
Worked solution and explanation
What this problem really is
This is top-N per group dressed up as a merchandising leaderboard, and the join and the SUM(quantity) are the easy part. The whole problem lives in one line: ROW_NUMBER() partitioned by category, ordered by total_quantity DESC and then by `product_name`. On this data several Automotive products sold exactly 6 units. Reach for RANK() and every one of them comes back at position 1; drop the name tiebreak and which 2 survive can change from run to run.
Break down the requirements
Step 1: Total units per product
Inner-join transactions to products on product_id and GROUP BY category, product_name, summing quantity into total_quantity. The inner join also drops sales whose product_id is NULL and products that never sold, so neither competes for a spot.
Step 2: Number products inside each category
Apply ROW_NUMBER() OVER (PARTITION BY category ORDER BY total_quantity DESC, product_name ASC) to the aggregate CTE. The partition restarts the count for every category, and product_name ASC is the tiebreak the statement asks for, so tied products get 1 and 2 in alphabetical order.
Step 3: Filter in an outer query
A window result is not visible to WHERE in the same SELECT, so the numbering lives in the ranked CTE and the outer query keeps category_rank <= 2. Row order is not graded, so no final sort is needed.
The solution
WITH product_qty AS (
SELECT prod.category,
prod.product_name,
SUM(txn.quantity) AS total_quantity
FROM transactions AS txn
INNER JOIN products AS prod ON txn.product_id = prod.product_id
GROUP BY prod.category, prod.product_name
),
ranked AS (
SELECT category,
product_name,
total_quantity,
ROW_NUMBER() OVER (
PARTITION BY category
ORDER BY total_quantity DESC, product_name ASC
) AS category_rank
FROM product_qty
)
SELECT category, product_name, total_quantity, category_rank
FROM ranked
WHERE category_rank <= 2WITH product_qty AS (
SELECT prod.category,
prod.product_name,
SUM(txn.quantity) AS total_quantity
FROM transactions AS txn
INNER JOIN products AS prod ON txn.product_id = prod.product_id
GROUP BY prod.category, prod.product_name
),
ranked AS (
SELECT category, product_name, total_quantity,
RANK() OVER (PARTITION BY category ORDER BY total_quantity DESC) AS category_rank
FROM product_qty
)
SELECT category, product_name, total_quantity, category_rank
FROM ranked
WHERE category_rank <= 2RANK() lets every product tied at the top share position 1, and without product_name nothing decides between them.
Common follow-up questions
- How would you keep every product tied for position 2, even when that means more than 2 rows in a category? (Swap to `RANK()`, keep `category_rank <= 2`, and explain how the row count grows.)
- How would you find the top 2 products per `category` and `transaction_date` to track daily winners? (Add `transaction_date` to the `GROUP BY` and to the `PARTITION BY`.)
- What changes if a product with `in_stock = 0` should be disqualified from the leaderboard? (Filter `products` before the join so disqualified products never accumulate units; filtering after numbering leaves gaps in the positions.)