Multiple JOINs
Concepts covered: Multiple JOINs
Real questions rarely stop at 2 tables. "Which products did each customer buy?" needs orders to connect customers to products. A query with 3 tables contains 2 joins, read in the order written. The first join produces a result table. The next join treats that result as its left side and attaches one more table to it. Each join is the same pairing step you already know, run again on a wider table.
Each order in the store's orders table also records the product sold, in a prod_id column the earlier examples left out.
- customers: cust_id VARCHAR PK, name VARCHAR
- orders: order_id VARCHAR PK, cust_id VARCHAR FK, prod_id VARCHAR FK, total INTEGER
- products: prod_id VARCHAR PK, product VARCHAR
- orders.cust_id references customers.cust_id (N:1)
- orders.prod_id references products.prod_id (N:1)
SELECT
c.name,
o.order_id,
p.product
FROM customers c
INNER JOIN orders o
ON c.cust_id = o.cust_id
INNER JOIN products p
ON o.prod_id = p.prod_id
ORDER BY o.order_id| cust_id | name |
|---|---|
| C1 | Alice |
| C2 | Bob |
| C3 | Carol |
| C4 | Dave |
| order_id | cust_id | prod_id | total |
|---|---|---|---|
| O1 | C1 | P1 | 50 |
| O2 | C1 | P2 | 30 |
| O3 | C3 | P1 | 75 |
| prod_id | product |
|---|---|
| P1 | Widget |
| P2 | Gadget |
| P3 | Gizmo |
| name | order_id | product |
|---|---|---|
| Alice | O1 | Widget |
| Alice | O2 | Gadget |
| Carol | O3 | Widget |
Trace the 3-table query one join at a time. customers joined to orders gives 3 rows: Alice with O1, Alice with O2, Carol with O3. Bob and Dave drop here. That 3-row result then joins to products on o.prod_id, a column that arrived with the first join. O1 and O3 both find Widget and O2 finds Gadget, so the count stays at 3. Gizmo, which no order names, never enters the result.
| After | Rows | Who is missing |
|---|---|---|
| FROM customers | 4 | Nobody |
| INNER JOIN orders | 3 | Bob and Dave, who have no orders |
| INNER JOIN products | 3 | Still Bob and Dave. Gizmo never joined. |
One INNER JOIN can undo a LEFT JOIN
Now the request changes: keep every customer, and show product names where orders exist. You switch the first join to LEFT JOIN and leave the product join as INNER JOIN. Bob and Dave survive the first join with NULL in o.prod_id. Then the product join asks whether NULL = p.prod_id for some product. A comparison with NULL is never true, so their rows find no pair, and an INNER JOIN drops rows with no pair. The customers you protected in step 1 are removed in step 2.
| cust_id | name |
|---|---|
| C1 | Alice |
| C2 | Bob |
| C3 | Carol |
| C4 | Dave |
| order_id | cust_id | prod_id | total |
|---|---|---|---|
| O1 | C1 | P1 | 50 |
| O2 | C1 | P2 | 30 |
| O3 | C3 | P1 | 75 |
| prod_id | product |
|---|---|
| P1 | Widget |
| P2 | Gadget |
| P3 | Gizmo |
LEFT, INNER
The INNER JOIN to products discards the padded rows for Bob and Dave. 3 rows come back.
SELECT
c.name,
o.order_id,
p.product
FROM customers c
LEFT JOIN orders o
ON c.cust_id = o.cust_id
INNER JOIN products p
ON o.prod_id = p.prod_id
ORDER BY c.name, o.order_id| name | order_id | product |
|---|---|---|
| Alice | O1 | Widget |
| Alice | O2 | Gadget |
| Carol | O3 | Widget |
LEFT, LEFT
Both joins are LEFT, so the padded rows pass through the product join untouched. 5 rows come back.
SELECT
c.name,
o.order_id,
p.product
FROM customers c
LEFT JOIN orders o
ON c.cust_id = o.cust_id
LEFT JOIN products p
ON o.prod_id = p.prod_id
ORDER BY c.name, o.order_id| name | order_id | product |
|---|---|---|
| Alice | O1 | Widget |
| Alice | O2 | Gadget |
| Bob | NULL | NULL |
| Carol | O3 | Widget |
| Dave | NULL | NULL |
So once a join is LEFT, every later join that hangs off the optional table must be LEFT too. Here products attaches through orders, and orders is optional, so the product join has to tolerate rows where orders is absent.
Count rows after every join
Long join chains fail quietly. A join that drops rows or multiplies them still returns a result that looks reasonable. The defense is to build the chain one join at a time. Start with the FROM table alone and note its row count. Add one join and predict the new count before running it. A drop means rows found no partner. A rise means the new table holds several rows per key. Either may be correct, and you should be able to say which before adding the next table.
A tickets table holds one row per support ticket, with a cust_id. Alice has 3 tickets and no other customer has any. With the store's 4 customers and 3 orders (2 for Alice, 1 for Carol), how many rows does this query return?
SELECT
c.name,
o.order_id,
t.ticket_id
FROM customers c
LEFT JOIN orders o
ON c.cust_id = o.cust_id
LEFT JOIN tickets t
ON c.cust_id = t.cust_id- 9 rows: Alice's 2 order rows each pair with her 3 tickets, and the other 3 customers have 1 row each
- 5 rows: the first join's 5 rows, since a LEFT JOIN cannot add rows to its left side
- 8 rows: the 5 rows from the first join plus 3 more, one for each of Alice's tickets
- 6 rows: Alice appears once per ticket, and the other 3 customers appear once each
Every technique in this lesson is the pairing model with one addition: a rule for rows that found no partner. The rule says which side's unmatched rows survive, if any, and you decide whether each condition acts before or after that rule runs. A chain of joins repeats the decision once per table.
About this lesson
This section is part of the Joins: Intermediate lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.