Best-selling product in each category
The buying team wants to know the best seller of each category, measured by total units sold in orders that were not cancelled.
Task: return the category, the product name and its total units sold, one row per category. If two products tie, pick the one whose name comes first alphabetically. Sort by category.
The data you can query
Each table is shown with its column types and the first rows, so you can see exactly what the data looks like.
| id INTEGER | name TEXT | city TEXT | signup_date TEXT |
|---|---|---|---|
| 1 | Ayesha Khan | Lahore | 2025-11-03 |
| 2 | Bilal Ahmed | Karachi | 2025-11-12 |
| 3 | Hina Malik | Islamabad | 2025-12-01 |
| 4 | Usman Tariq | Rawalpindi | 2025-12-15 |
| 5 | Sana Iqbal | Lahore | 2026-01-04 |
| 6 | Omar Farooq | Faisalabad | 2026-01-20 |
Showing the first 6 of 14 rows.
| id INTEGER | order_id INTEGER | product_id INTEGER | quantity INTEGER |
|---|---|---|---|
| 1 | 1 | 1 | 2 |
| 2 | 1 | 2 | 4 |
| 3 | 2 | 6 | 1 |
| 4 | 2 | 2 | 4 |
| 5 | 2 | 10 | 4 |
| 6 | 3 | 9 | 1 |
Showing the first 6 of 80 rows.
| id INTEGER | customer_id INTEGER | order_date TEXT | status TEXT |
|---|---|---|---|
| 1 | 3 | 2026-01-16 | Delivered |
| 2 | 11 | 2026-06-14 | Delivered |
| 3 | 7 | 2026-02-15 | Delivered |
| 4 | 10 | 2026-06-19 | Delivered |
| 5 | 5 | 2026-01-06 | Delivered |
| 6 | 9 | 2026-03-06 | Delivered |
Showing the first 6 of 40 rows.
| id INTEGER | name TEXT | category TEXT | price REAL |
|---|---|---|---|
| 1 | Wireless Mouse | Electronics | 1850 |
| 2 | USB Cable | Electronics | 450 |
| 3 | Bluetooth Speaker | Electronics | 4200 |
| 4 | Notebook A4 | Stationery | 220 |
| 5 | Gel Pen Pack | Stationery | 180 |
| 6 | Desk Organizer | Stationery | 950 |
Showing the first 6 of 10 rows.
Your query runs on SQLite. Write a single SELECT; use LIMIT instead of TOP.
Need a hint?
Sum units per product first, then number the products inside each category with ROW_NUMBER() OVER (PARTITION BY category ORDER BY units DESC, name).
Log in or create a free account to run your code, submit and earn points.
Run your code to see the output here. Ctrl + Enter also runs it.
