⌨ Programming Easy SQL Joins

Total Revenue by Product Category

Question

Calculate total revenue generated by each product category. Return category name and total revenue, highest first.

πŸ’‘ Join categories, products, and order_items.
SQL Editor
Solution
SELECT
  cat.category_name,
  SUM(oi.quantity * oi.unit_price) AS total_revenue
FROM categories cat
JOIN products p ON cat.category_id = p.category_id
JOIN order_items oi ON p.product_id = oi.product_id
GROUP BY cat.category_id, cat.category_name
ORDER BY total_revenue DESC;
Explanation: Chain three JOINs from categories β†’ products β†’ order_items. Revenue = quantity Γ— unit_price. Group by category.