DEVELOPED COURSE TEXT
Join cardinality and duplicate counting
A join creates combinations, not a business total
When an order has two items and one payment, joining them produces two rows. When another has one item and two payments, it also produces two rows. Joining two child tables can multiply their records within each parent. The engine is following the relationship conditions correctly; the analytical mistake is treating every repeated value as an additional event. Inspect the cardinality and define the desired output grain before summing.
A deliberately wrong total is an executable counterexample
The following query is intentionally wrong for payment totals or line totals because it joins item and payment grains before aggregation. It is useful as a reproducible failure case, not as a dashboard query.
SELECT SUM(p.amount_cents) AS duplicated_payment_sum,
SUM(i.quantity * i.unit_price_cents) AS duplicated_item_sum
FROM orders AS o
JOIN order_items AS i ON i.order_id = o.order_id
JOIN payments AS p ON p.order_id = o.order_id
WHERE o.status = 'paid';
Aggregate independent child facts before combining
Reduce each child table to one row per target parent, then join those summaries when the metric calls for an order-level result. COUNT(DISTINCT order_id) can repair a particular entity count but does not repair arbitrary sums. SUM(DISTINCT amount) is especially dangerous: two legitimate events with the same amount can be collapsed. The query below returns 4,600 cents because the two separate 1,000-cent payments become one distinct numeric value.
SELECT SUM(DISTINCT amount_cents) FROM payments;
Keep zero-order customers through the intended outer join
Put the eligible-order condition in the join when the contract includes every customer, including those with no paid order. A WHERE condition on the nullable order side can remove those unmatched rows.
SELECT c.customer_id, c.name, COUNT(o.order_id) AS paid_orders
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id AND o.status = 'paid'
GROUP BY c.customer_id, c.name
ORDER BY c.customer_id;
Worked records
Record 1
The naive child-table join reports payment sum 8,100 cents and item-line sum 7,700 for the paid-order cohort. The original payments total 5,600 and the correct paid line amount is also 5,600.
Develop the reasoning
Order 1’s one payment of 2,500 repeats for its two items, contributing 5,000 to the naive payment sum. Order 3’s 2,100 item amount repeats for its two payments, contributing 4,200 to the naive line sum. These are different duplication paths. Distinct entity counts cannot generally fix them, and distinct monetary values erase genuine equal-valued events. Aggregate each fact at its own grain and validate expected row counts before combining. The customer outer join returns paid-order counts Alpha 1, Beta 2, Gamma 0 and Delta 0; a paid-only WHERE condition would lose the zero-order customers.
Progressive practice — optional
1. Foundation
Why is the naive payment total too high for order 1?
Compare the worked reasoning
The same payment repeats once per joined item; it is not a second cash receipt.
Change the record
A third item is added without another payment.
Compare the changed-record reasoning
The naive payment contribution increases again even though actual cash does not, exposing the grain error.
2. Application
Why does SUM(DISTINCT amount_cents) return 4,600 rather than 5,600?
Compare the worked reasoning
It keeps only distinct numeric amounts 2,500, 1,000 and 1,100, collapsing two legitimate 1,000-cent events.
Change the record
Two additional distinct payments also equal 1,000.
Compare the changed-record reasoning
The method loses them too. Event identity, not amount uniqueness, determines whether records are duplicates.
3. Transfer
Where should the paid-status condition be applied if customers with zero paid orders must remain?
Compare the worked reasoning
In the eligible-order join or a prefiltered order subquery, while preserving the outer customer set.
Change the record
A WHERE predicate then requires a non-NULL order.
Compare the changed-record reasoning
That removes unmatched customers and changes the contract; explicitly test zero-order fixtures.
Sources and scope
- SQLite documentation: SQL language behavior for the explicitly named SQLite examples; portability to another engine requires checking that engine.
- SQLite — Built-in Aggregate Functions: COUNT, SUM, DISTINCT and empty-input behavior.