Joins with totals
Joins and grouping together answer the questions businesses actually ask: who are our best customers, which products sell, which city buys the most?
The value of an order line is its quantity times the product's price. That needs columns from two tables, so join first, then group:
SELECT p.category, SUM(o.quantity) AS items_sold
FROM orders AS o
JOIN products AS p ON p.id = o.product_id
GROUP BY p.category
ORDER BY items_sold DESC;
You can put expressions inside aggregates, like SUM(o.quantity * p.price), and order by the name you gave a column with AS.
Think of it in steps:
FROMandJOINbuild one wide table of order lines with all the facts.GROUP BYcollects those lines into one bucket per customer.SELECTworks out the summary for each bucket.ORDER BYsorts the buckets.
Your task
For each customer who has placed an order, show their name as customer and the total amount they have spent as total (quantity times price, summed, rounded to 2 decimals). Sort from the biggest spender down.