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

transactions: 10 of 200 rows
transaction_iduser_idproduct_idquantitytotal_amounttransaction_date
10671971001223.462026-02-02
11342941050336.932026-03-03
12013911099450.42026-04-04
12684881148563.872026-05-05
13355851197002026-06-06
14026821246290.812026-07-07
146977912953104.282026-08-08
153687613444117.752026-09-09
160397313935-452026-01-10
1670107014421144.692026-02-11

products

Columns: product_id INTEGER, product_name TEXT, category TEXT, price REAL, rating REAL, in_stock BOOLEAN

products: 10 of 200 rows
product_idproduct_namecategorypriceratingin_stock
1001Basic Device 1XBooks17.522.71
1050Deluxe Bundle 2XClothing25.054.41
1099Pro Unit 3XHome & Kitchen32.582.11
1148Ultra Tool 4XCollectibles40.113.81
1197Essential Set 5XToys47.641.51
1246Classic System 6XBeauty03.21
1295Smart Gadget 7XAutomotive62.74.90
1344Eco Kit 8XGarden70.232.61
1393Turbo Pack 9XMusic77.764.30
1442Premium Widget 10XElectronics85.2921

Expected output

First 5 of 22 rows
categoryproduct_nametotal_quantitycategory_rank
AutomotiveSmart Gadget 27X61
AutomotiveSmart Gadget 37X62
BeautyClassic System 26X41
BeautyClassic System 36X42
BooksBasic Device 1X61

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

Aggregate, number within `category`, keep the top 2
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 <= 2
The tempting version that breaks on ties
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,
         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 <= 2

RANK() lets every product tied at the top share position 1, and without product_name nothing decides between them.

Common follow-up questions