Difficulty: Beginner → Early Intermediate
Estimated time: 15–20 minutes
SQL concepts: SUM(), GROUP BY, ORDER BY, LIMIT
Goal: Return only the top N groups based on an aggregated metric.
The Challenge
The product manager asks:
“Which are our top 3 product categories by total value?”
This is a classic prioritization question used in:
- dashboards
- business reviews
- inventory decisions
- category performance reporting
To answer it, you must:
- Aggregate totals per category
- Sort totals from highest to lowest
- Return only the top 3
Database Table
products
| column_name | type | description |
|---|---|---|
| product_id | INTEGER | Unique product ID |
| name | TEXT | Product name |
| category | TEXT | Product category |
| price | DECIMAL | Product price |
Sample Data
| product_id | name | category | price |
|---|---|---|---|
| 101 | Wireless Mouse | Accessories | 24.99 |
| 102 | Mechanical Keyboard | Accessories | 89.00 |
| 103 | Webcam | Accessories | 59.99 |
| 104 | Monitor | Displays | 229.99 |
| 105 | USB-C Hub | Accessories | 34.50 |
| 106 | Laptop Stand | Workspace | 39.99 |
| 107 | Docking Station | Accessories | 129.00 |
| 108 | Chair Mat | Workspace | 79.99 |
| 109 | 34-inch Monitor | Displays | 399.00 |
Your Task
Write a SQL query that returns:
- Each category
- The total value of products in that category
- Sorted from highest to lowest total value
- Only the top 3 categories
Expected Output
| category | total_value |
|---|---|
| Displays | 628.99 |
| Accessories | 337.48 |
| Workspace | 119.98 |
(Exact totals depend on data; the Top-3 logic is the goal.)
Constraints
- Use
SUM() - Use
GROUP BY - Use
ORDER BYon the aggregated total - Use
LIMITto return only the top results - Do not use
SELECT *
Hint (Optional)
The correct order is:
GROUP BY- aggregate (e.g.,
SUM) ORDER BYLIMIT
If you apply LIMIT without sorting, you won’t get the “top” results.
SELECT
category,
SUM(price) AS total_value
FROM products
GROUP BY category
ORDER BY total_value DESC
LIMIT 3;
Explanation
GROUP BY categorycreates one row per categorySUM(price)calculates total value per categoryORDER BY total_value DESCranks categories from highest to lowest totalLIMIT 3keeps only the top 3 categories
This is one of the most common patterns in analytics.
Key Insight
Top-N reporting is just: Aggregate → Sort → Limit
Once learners can do this, they can answer many “what matters most?” business questions.
Common Mistakes
- Forgetting
DESCand ranking lowest totals first - Using
LIMITwithoutORDER BY - Ordering by
priceinstead ofSUM(price)
Optional Extension (Mini Bonus)
Try these variations:
- Show the top 1 category only
- Show the top 3 categories by average price
- Show the bottom 3 categories (lowest total value)
🔗 Next Challenge
Beginner Challenge #30: Top-N with Ties (Handling Equal Totals)
Why this challenge matters
This is a direct bridge to real dashboards, where leaders ask:
“What are the top performers?”
Top-N queries are everywhere—and now your learners can write them.
🔗 View reference solution on GitHub
(After you’ve tried the challenge)
Want more practical SQL challenges?
Subscribe to the Solve With SQL newsletter and get new problems delivered to your inbox.