Before you start, you need
Summarise without multiplying a measure
Joining tables can repeat a figure without there being any more money. Picture an order of 100 with three different products: when you join the order to its three lines, the total of 100 appears three times. If you add it up, you get 300 by mistake.
Decide what each row represents before adding. If you want the amount per customer, join customers and orders: each order must contribute its total only once. To calculate sales per product, use the quantities and prices on the lines.
In the example we keep every customer and add up their non-cancelled orders. GROUP BY c.id, c.name produces one row per customer. Using their id avoids mixing up people who share a name.
If a customer has no matching orders, SUM returns NULL. COALESCE lets you show a fallback: COALESCE(SUM(p.total), 0) returns the sum if there is one, and 0 if not. Do not use DISTINCT to hide repeated amounts: check which tables the question really needed.
SELECT key, SUM(measure)
FROM ...
GROUP BY key;See it in action
What do each customer's non-cancelled orders add up to, including customers with none?
SELECT c.name,
COALESCE(SUM(p.total), 0) AS total
FROM customers AS c
LEFT JOIN orders AS p
ON c.id = p.customer_id
AND p.status <> 'cancelled'
GROUP BY c.id, c.name;| name | total |
|---|---|
| Amy Wilson | 377.4 |
| Liam Martin | 253.4 |
| Megan Reed | 215.9 |
| David Clark | 69.9 |
| Sara Green | 1112 |
| Peter Hill | 0 |
| Laura Ward | 148.9 |
| Hugh Miller | 268 |
- Every customer is kept and their valid orders are looked up. The intermediate output has one row per customer-order pair.
- GROUP BY c.id, c.name produces one row per customer. SUM(p.total) adds each order once.
- If there are no matching orders, the sum is NULL. COALESCE(..., 0) shows 0 in that case: it returns the first value that is present.
- Do not join the order lines if you are going to add up the header total. Each line would repeat that figure and could inflate it.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Add up orders per customer
Write a query that returns every customer and their non-cancelled total; use 0 when they have no valid orders.
Expected result: 12 rows with the columns name, total, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
Add up each valid order only once, and keep the customers with no matches.
Use LEFT JOIN to keep them.
Filter out cancelled orders inside ON and group by customer.
Solution explained
SELECT c.name, COALESCE(SUM(p.total), 0) AS total FROM customers AS c LEFT JOIN orders AS p ON c.id = p.customer_id AND p.status <> 'cancelled' GROUP BY c.id, c.name;Putting the filter in ON keeps customers with no valid orders; COALESCE turns the missing sum into zero.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Check the detail before the summary
Show order_id, the product name and the quantity for each line of order_items. Join order_items to products.
Expected result: 27 rows with the columns order_id, name, quantity, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
The unit you need is the line, not the order or the distinct product.
The product key is in order_items.
Join d.product_id to pr.id.
Solution explained
SELECT d.order_id, pr.name, d.quantity FROM order_items AS d JOIN products AS pr ON d.product_id = pr.id;Each row represents one line of an order. A product can legitimately appear on several lines.
Key takeaways
- Define what a row represents before and after joining.
- A join can repeat measures from the original table.
- Group at the level the question asks for.
- DISTINCT does not fix a multiplied sum.