Sub topic 10
Counting and Summing with Outer Joins
Assume you know SELECT, WHERE, GROUP BY, primary and foreign keys, NULL, and INNER JOIN. This chapter focuses on a common task: reporting every customer, including customers who have no orders.
A LEFT JOIN first preserves every row from the left table. It then adds matching rows from the right table. If no match exists, the right-side columns contain NULL.
Count matched rows correctly
Suppose the tables contain these rows:
| Customer | Orders | Order totals |
|---|---|---|
| Ada | 2 | 50.00, 20.00 |
| Ben | 1 | 35.00 |
| Cleo | 0 | — |
To report every customer, group the joined rows by customer:
SELECT
c.customer_id,
c.name,
COUNT(o.order_id) AS order_count,
SUM(o.total) AS total_spent
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
GROUP BY c.customer_id, c.name
ORDER BY c.customer_id;The result is:
| customer_id | name | order_count | total_spent |
|---|---|---|---|
| 1 | Ada | 2 | 70.00 |
| 2 | Ben | 1 | 35.00 |
| 3 | Cleo | 0 | NULL |
COUNT(o.order_id) counts only non-NULL order IDs. Cleo receives one NULL-padded row, so the count is zero.
COUNT(*) behaves differently:
SELECT
c.name,
COUNT(*) AS joined_rows,
COUNT(o.order_id) AS order_count
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
GROUP BY c.customer_id, c.name;For Cleo, COUNT(*) is 1 because the joined result contains one customer row. COUNT(o.order_id) is 0 because the order ID is NULL.
Turn a NULL sum into zero
SUM ignores NULL values, but it returns NULL when every value in a group is NULL. That is why Cleo's total_spent is NULL, not zero.
Use COALESCE, which returns the first non-NULL value:
SELECT
c.name,
COUNT(o.order_id) AS order_count,
COALESCE(SUM(o.total), 0) AS total_spent
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
GROUP BY c.customer_id, c.name
ORDER BY c.customer_id;Now Cleo has order_count = 0 and total_spent = 0.
The same pattern works for other aggregates. For example, AVG(o.total) is NULL for a customer without orders. Decide whether that meaning is useful, or replace it with zero or another display value.
Avoid changing the meaning of the join
Filters can accidentally remove customers without orders:
-- Cleo disappears
SELECT c.name, SUM(o.total) AS total_spent
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
WHERE o.total >= 30
GROUP BY c.customer_id, c.name;The WHERE clause runs after the join. It rejects Cleo's NULL-padded row. If the condition describes which orders may match, put it in ON instead:
SELECT
c.name,
COUNT(o.order_id) AS large_order_count,
COALESCE(SUM(o.total), 0) AS large_order_total
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
AND o.total >= 30
GROUP BY c.customer_id, c.name;This preserves Cleo while counting only orders of at least 30.
Exercise
Exercise
Write a query that lists every customer and counts only orders with a total greater than 25. Customers without such orders must appear with a count of 0.
Show answer
SELECT c.name, COUNT(o.orderid) AS qualifyingorders FROM customers AS c LEFT JOIN orders AS o ON o.customerid = c.customerid AND o.total > 25 GROUP BY c.customer_id, c.name;