Sub topic 11
Aggregating Across Joins Without Inflating Totals
Assume you know basic SELECT, WHERE, GROUP BY, INNER JOIN, and LEFT JOIN. This chapter focuses on one common aggregation error: joining tables first and then summing values from a table whose rows are repeated by the join.
Why joins can inflate totals
Suppose each customer can have many orders, and each order can have many payments:
customers(customer_id, name)orders(order_id, customer_id, amount)payments(payment_id, order_id, amount)
An order with three payments appears three times after joining orders to payments. If you then calculate SUM(orders.amount), the order amount is added three times.
SELECT o.customer_id,
SUM(o.amount) AS order_total,
SUM(p.amount) AS payment_total
FROM orders AS o
JOIN payments AS p ON p.order_id = o.order_id
GROUP BY o.customer_id;The payment total may be correct, but the order total is inflated because the join creates one result row per payment.
Pre-aggregate before joining
Aggregate each many-side table to the level you need before combining the results. The following query first creates one row per customer in each subquery. The final join therefore matches one customer summary to another customer summary.
SELECT c.customer_id,
c.name,
COALESCE(o.order_total, 0) AS order_total,
COALESCE(p.payment_total, 0) AS payment_total
FROM customers AS c
LEFT JOIN (
SELECT customer_id,
SUM(amount) AS order_total
FROM orders
GROUP BY customer_id
) AS o ON o.customer_id = c.customer_id
LEFT JOIN (
SELECT o.customer_id,
SUM(p.amount) AS payment_total
FROM orders AS o
JOIN payments AS p ON p.order_id = o.order_id
GROUP BY o.customer_id
) AS p ON p.customer_id = c.customer_id;COALESCE replaces NULL with zero for customers who have no matching orders or payments. A LEFT JOIN keeps customers even when their summary subquery has no row.
Aggregate at the required grain
The grain is the level represented by one row. If the report needs one row per order, aggregate payments by order_id first. If it needs one row per customer, aggregate orders and payments by customer_id.
SELECT o.order_id,
o.customer_id,
o.amount,
COALESCE(p.payment_total, 0) AS payment_total
FROM orders AS o
LEFT JOIN (
SELECT order_id,
SUM(amount) AS payment_total
FROM payments
GROUP BY order_id
) AS p ON p.order_id = o.order_id;This pattern also avoids a second problem: joining two independent one-to-many tables can multiply rows in both directions. For example, four orders and three contacts for one customer can produce twelve joined rows. Aggregating each table separately prevents that fan-out.
Checks for inflated results
Compare the total before and after a join. Count rows at each stage, and inspect a key that should be unique. If one order appears several times, do not fix the result with SUM(DISTINCT amount) unless duplicate amounts are genuinely interchangeable. Two different orders can have the same amount, and DISTINCT would incorrectly remove one of them.
Exercise
A customer has two orders worth 100 each. Joining those orders to three payment rows causes repeated order rows. Why is SUM(DISTINCT amount) unsafe, and what should you do instead?
Show answer
It is unsafe because the two different orders have the same amount, so DISTINCT would count 100 only once. Aggregate orders and payments separately at the needed grain, then join the summaries.