SolveWithSQL

SQL Beginner Challenge 29: Limiting Grouped Results (Top-N with Aggregates)

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:

  1. Aggregate totals per category
  2. Sort totals from highest to lowest
  3. Return only the top 3

Database Table

products

column_nametypedescription
product_idINTEGERUnique product ID
nameTEXTProduct name
categoryTEXTProduct category
priceDECIMALProduct price

Sample Data

product_idnamecategoryprice
101Wireless MouseAccessories24.99
102Mechanical KeyboardAccessories89.00
103WebcamAccessories59.99
104MonitorDisplays229.99
105USB-C HubAccessories34.50
106Laptop StandWorkspace39.99
107Docking StationAccessories129.00
108Chair MatWorkspace79.99
10934-inch MonitorDisplays399.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

categorytotal_value
Displays628.99
Accessories337.48
Workspace119.98

(Exact totals depend on data; the Top-3 logic is the goal.)

Constraints

  • Use SUM()
  • Use GROUP BY
  • Use ORDER BY on the aggregated total
  • Use LIMIT to return only the top results
  • Do not use SELECT *

Hint (Optional)

The correct order is:

  1. GROUP BY
  2. aggregate (e.g., SUM)
  3. ORDER BY
  4. LIMIT

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 category creates one row per category
  • SUM(price) calculates total value per category
  • ORDER BY total_value DESC ranks categories from highest to lowest total
  • LIMIT 3 keeps 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 DESC and ranking lowest totals first
  • Using LIMIT without ORDER BY
  • Ordering by price instead of SUM(price)

Optional Extension (Mini Bonus)

Try these variations:

  1. Show the top 1 category only
  2. Show the top 3 categories by average price
  3. 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.