Calculate Revenue and Units Sold per Product Category
Report orders, units sold, revenue and average price for each product category.
The challenge runs on the SQLabHub practice database (SQLite) and uses the products and order_items tables. You can solve it in the browser, with nothing to install.
The task
Calculate total revenue, units sold and number of orders per product category.
A correct answer returns 10 rows with 5 columns: category, total_orders, units_sold, total_revenue, avg_price.
Tables used in this challenge
products 120 rows
id INTEGER (PK) · name TEXT · category TEXT · price REAL · cost REAL · stock INTEGER · sku TEXT · status TEXT · rating REAL · review_count INTEGER
| id | name | category | price | cost | stock | sku | status | rating | review_count |
|---|---|---|---|---|---|---|---|---|---|
order_items 496 rows
id INTEGER (PK) · order_id INTEGER · product_id INTEGER · quantity INTEGER · unit_price REAL · subtotal REAL
| id | order_id | product_id | quantity | unit_price | subtotal |
|---|---|---|---|---|---|
The first rows of each table are shown. See how all the tables relate on the database schema page.
Expected output
| category | total_orders | units_sold | total_revenue | avg_price |
|---|---|---|---|---|
The complete result: 10 rows. This is the output of the model solution on the practice database.
What you practise
- Summarising many rows into one value with COUNT, SUM, AVG, MIN or MAX
- Counting different values with COUNT(DISTINCT …)
- Grouping rows with GROUP BY to get one result row per group
- Combining rows from two tables on a matching key with an inner join
- Sorting the result with ORDER BY
Hint and approach
Try the challenge first. Open these only when you need them.
Show the hint
JOIN order_items with products, then GROUP BY category
Show the step-by-step approach
How the model solution is built, in the order the database evaluates it:
- Read from the
order_itemstable. - Join
productsonproducts.id=order_items.product_id. - Group the rows by
products.category, so the result has one row for each value. - For each group, compute
COUNT(DISTINCT order_items.order_id), which counts each different value once,SUM(order_items.quantity),SUM(order_items.subtotal)andAVG(products.price). ROUND()trims the calculated figures to a fixed number of decimals.- Sort the result by
total_revenuedescending.
Model solution
Checking your session…
What to watch for
One order can contain several lines in the same category, so orders are counted with COUNT(DISTINCT order_id). The average price here is weighted by how often a product was sold, because it is averaged over order lines rather than over products.
- INNER JOIN. An inner join drops rows that have no partner on the other side, and it repeats a row once for every match, which inflates SUM and COUNT.
- COUNT(DISTINCT). After a join, COUNT(*) counts joined rows. Counting the parent entity needs COUNT(DISTINCT its id).
- GROUP BY. Every selected column must either be in GROUP BY or sit inside an aggregate. SQLite accepts other columns and returns an arbitrary value for them; most databases raise an error.
Related SQL challenges
- Count Distinct Products per OrderSimilar levelMedium
- Calculate the Average Review Rating per Product CategorySimilar levelMedium
- Find the Lowest Price in Each CategoryEasierEasy
- Count Distinct Products Bought by Each CustomerHarderHard
- Calculate Average Items per Order for Each CustomerHarderHard
Keep learning
- Lesson: INNER JOIN
- Lesson: Aggregate Functions
- Lesson: GROUP BY
- Lesson: ORDER BY
- All SQL practice challenges, by difficulty and topic
- SQL interview questions with answers
- SQL Playground: run any query on the same database