Home / SQL / Practice problems / Medium

Revenue per category

Mediumrow order matters

Show each product category and its revenue (sum of quantity × unit_price over all order items), highest first.

Tables: customers(customer_id, name, city, country, joined) · products(product_id, name, category, price) · orders(order_id, customer_id, order_date, status) · order_items(order_id, product_id, quantity, unit_price)

Your query
Show the expected result
categoryrevenue
Electronics11502.0
Furniture8153.0
Books2638.0
Grocery824.0
Stationery339.0

5 rows

Show a solution
SELECT p.category, SUM(i.quantity * i.unit_price) AS revenue FROM order_items i JOIN products p ON p.product_id = i.product_id GROUP BY p.category ORDER BY revenue DESC;

SQL used

New to SQL? The free SQL course teaches everything these problems use, with a self-checking exercise in every lesson.