Project: sales questions
Use everything to answer what a shop owner asks.
Time to put the pieces together. The shop owner has questions, and each one is a query: decide which tables hold the answer, how they join, which rows to keep, how to group, and how to sort. Revenue means quantity times price, added up. Cancelled orders do not count as sales.
SELECT p.category, SUM(oi.quantity * p.price) AS revenue
FROM order_items oi
JOIN orders o ON oi.order_id = o.id
JOIN products p ON oi.product_id = p.id
WHERE o.status <> 'cancelled'
GROUP BY p.category;Joins items to their orders (to check status) and products (for price and category), drops cancelled orders, then adds up revenue per category.
Build each answer in steps: join, check the rows with SELECT *, then add WHERE, GROUP BY and ORDER BY one at a time.