A visual, row-by-row guide to INNER, LEFT, FULL OUTER JOINs and how joining parent and child tables at different grains causes silent revenue duplication (the Fan-Out Trap).
Imagine you have two clipboards at a wedding:
- Clipboard A (`orders`): 1 line per Order (Order #101: Total Shipping Fee = $10).
- Clipboard B (`order_items`): 3 lines for Order #101 (Shoes, Hat, Belt).
If you staple every item line directly onto the Order clipboard (JOIN), Order `#101` gets cloned 3 times, and the $10 Shipping Fee suddenly shows up on 3 rows ($10 + $10 + $10 = $30)! You accidentally tripled your shipping revenue.
That accidental cloning is called a SQL Fan-Out Trap (or cardinality explosion). Understanding joins means tracking "what 1 row represents" before and after every JOIN.
SUM() or COUNT() on left-table metrics!| JOIN Type | What Survives in Result? | Missing Match Behavior | Everyday Use Case |
|---|---|---|---|
| `INNER JOIN` | Only rows with a matching key in both tables. | Drops unmatched rows from both sides. | Show only orders placed by verified registered users. |
| `LEFT JOIN` | All rows from Left table + matches from Right. | Right columns become NULL if no match. | Keep all customers—even those with 0 orders yet. |
| `FULL OUTER JOIN` | All rows from both Left and Right tables. | Unmatched cells on either side become NULL. | Finance reconciliation (Stripe payouts vs. Bank ledger). |
| `CROSS JOIN` | Every left row paired with every right row (N x M). | No ON clause. Dangerous by accident, great for calendars! | Generate 365 days x 50 stores grid before left-joining sales. |
| Semi-Join (`EXISTS`) | Left rows that have >= 1 match in Right (no cloning!). | Never duplicates Left rows even on 1-to-many matches. | Find customers who bought *at least one* item cleanly. |
Let's test this on real sample tables so you can watch the bug happen with your own eyes.
| order_id | customer_id | shipping_fee |
|---|---|---|
| 101 | C1 | 10.00 |
| 102 | C1 | 15.00 |
True total shipping fee for `C1` = `$10.00 + $15.00 = $25.00`.
| item_id | order_id | product | item_price |
|---|---|---|---|
| 1 | 101 | Keyboard | 80.00 |
| 2 | 101 | Mouse | 30.00 |
| 3 | 101 | USB Cable | 10.00 |
| 4 | 102 | Monitor | 250.00 |
SELECT
o.customer_id,
SUM(o.shipping_fee) AS reported_shipping,
SUM(i.item_price) AS reported_items
FROM orders o
JOIN order_items i
ON o.order_id = i.order_id
GROUP BY o.customer_id;Look at what the intermediate JOIN builds *before* GROUP BY sums anything:
| o.order_id | o.shipping_fee | i.item_id | i.item_price |
|---|---|---|---|
| 101 | 10.00 | 1 | 80.00 |
| 101 | 10.00 | 2 | 30.00 |
| 101 | 10.00 | 3 | 10.00 |
| 102 | 15.00 | 4 | 250.00 |
reported_items = 80 + 30 + 10 + 250 = $370.00 (correct!)reported_shipping = 10 + 10 + 10 + 15 = $45.00 (WRONG! Inflated by $20!)Before joining two tables, make sure their grains match (1 row per order_id on BOTH sides!):
WITH item_totals_by_order AS (
SELECT
order_id,
COUNT(*) AS item_count,
SUM(item_price) AS items_subtotal
FROM order_items
GROUP BY order_id
)
SELECT
o.customer_id,
COUNT(o.order_id) AS order_count,
SUM(o.shipping_fee) AS true_shipping_fee,
SUM(COALESCE(it.items_subtotal, 0)) AS true_items_total
FROM orders o
LEFT JOIN item_totals_by_order it
ON o.order_id = it.order_id
GROUP BY o.customer_id;Output: true_shipping_fee = 25.00, true_items_total = 370.00 — 100% accurate, zero duplication!
SELECT c.customer_name, o.order_id
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE o.status = 'PAID';o.status = NULL. Putting o.status = 'PAID' in the WHERE clause deletes all unmatched customers!LEFT JOIN orders o
ON c.customer_id = o.customer_id
AND o.status = 'PAID'JOIN (SELECT COUNT(*)) to spot unexpected fan-out immediately.WHERE) when using LEFT JOIN.1-to-Many child tables at the same time (the Chasm Trap) — aggregate each child table in its own CTE first.Test your understanding of the concepts covered in this lesson.
For each customer_id, compute accurate total_shipping (from gold.fact_orders) and total_items_value (from gold.fact_order_items) without inflating shipping_fee! Pre-aggregate shipping per customer in one CTE and item revenue per customer in a second CTE, then join them by customer_id.