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.

Schema
  • 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
customers
cust_idname
C1Alice
C2Bob
C3Carol
C4Dave
orders
order_idcust_idprod_idtotal
O1C1P150
O2C1P230
O3C3P175
products
prod_idproduct
P1Widget
P2Gadget
P3Gizmo
Result
nameorder_idproduct
AliceO1Widget
AliceO2Gadget
CarolO3Widget

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.

AfterRowsWho is missing
FROM customers4Nobody
INNER JOIN orders3Bob and Dave, who have no orders
INNER JOIN products3Still 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.

customers
cust_idname
C1Alice
C2Bob
C3Carol
C4Dave
orders
order_idcust_idprod_idtotal
O1C1P150
O2C1P230
O3C3P175
products
prod_idproduct
P1Widget
P2Gadget
P3Gizmo

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
Result
nameorder_idproduct
AliceO1Widget
AliceO2Gadget
CarolO3Widget

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
Result
nameorder_idproduct
AliceO1Widget
AliceO2Gadget
BobNULLNULL
CarolO3Widget
DaveNULLNULL

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

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.