First Among Equals
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
Problem
The merchandising team wants to see the products that move the most units, counting every unit across all of a product's orders. Return each product whose unit total matches the highest any product reached, showing how many orders it took and the units it moved.
When more than one product wears the crown, name every one of them.
Tables
transactions
Columns: transaction_id INTEGER, user_id INTEGER, product_id INTEGER, quantity INTEGER, total_amount REAL, transaction_date TEXT
| 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 INTEGER
| 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
| product_name | order_count | units_sold |
|---|---|---|
| Ultra Tool 4X | 2 | 10 |
| Turbo Pack 9X | 2 | 10 |
| Ultra Tool 14X | 2 | 10 |
| Turbo Pack 19X | 2 | 10 |
| Ultra Tool 24X | 2 | 10 |
Worked solution and explanation
What this really is
Strip the leaderboard costume off and this is an argmax that refuses to drop its ties. You total each product's units, then return every product sitting at the top total, not one arbitrary winner. Two things sink most attempts. First, ranking by the number of orders instead of units: on this data every product has the same order count, so a count can never separate them and the whole catalog ties. Second, reaching for 'sort descending, take the top row', which silently throws away the co-leaders. The correct shape is two passes: learn the winning total, then return everyone who hit it.
The naive answer and why it fails
- Naive: LIMIT 1
- GROUP BY product ORDER BY SUM(quantity) DESC LIMIT 1. It returns one product and looks right. The instant a second product ties on units, that product vanishes from the result with no error and no warning. You would ship a best-seller report that hides half the winners.
- Correct: equal to the max
- GROUP BY product, then HAVING SUM(quantity) = (the max unit total across all products). Every product at the peak survives, whether that is one product or fifty. The result is stable no matter how many share the lead.
SELECT p.product_name, COUNT(*) AS order_count, SUM(t.quantity) AS units_sold
FROM transactions t
JOIN products p ON t.product_id = p.product_id
GROUP BY p.product_id, p.product_name
HAVING SUM(t.quantity) = (
SELECT MAX(units) FROM (
SELECT SUM(t2.quantity) AS units
FROM transactions t2
JOIN products p2 ON t2.product_id = p2.product_id
GROUP BY t2.product_id
)
)Total units per product, then a scalar max in HAVING keeps every co-leader.
Step 1: Total each product's units
Join to products on product_id for the name, group per product, and add up quantity for the unit total. COUNT(*) rides along as context: how many orders produced those units. Notice this is SUM(quantity), not COUNT(*): counting orders answers a different question, and here every product has the same order count, so a count could never rank them.
Step 2: Find the winning total as a scalar
The inner query repeats the same per-product unit total, then wraps it in MAX(units) to collapse all those totals into one number: the peak. It joins to products for the same reason the outer query does. Skip that join and the max is computed over product_ids that may not exist in the catalog, so the peak can belong to a product that never appears in the joined output, and nothing matches. It has to be its own aggregation because you cannot take MAX of an already grouped SUM in one level without a nested query.
Step 3: Keep only the products at the peak
HAVING SUM(quantity) = (that scalar) filters the grouped rows down to exactly the products sitting at the top. Ties survive naturally because equality does not care how many rows satisfy it. ORDER BY product_name then gives the stable alphabetical output the preview shows.
Common follow-up questions
- How would you also return the second-best tier of products, not just the top? (Pushes the candidate from a scalar max toward `DENSE_RANK` over the per-product totals.)
- What changes if best-selling should mean the most revenue instead of the most units? (Tests whether they can swap SUM(quantity) for SUM(`total_amount`) and understand the metric distinction.)
- How would you break ties and return exactly one product deterministically? (Explores tie-break keys such as highest `total_amount` or earliest `transaction_date`.)