Sub topic 11
Chaining Several Joins
This chapter assumes you know SELECT, FROM, WHERE, NULL, foreign keys, and inner joins. It also assumes you understand that an outer join preserves rows from one side and fills missing columns with NULL.
A query can join more than two tables. Each join adds another matching step, but it also adds another opportunity to remove or multiply rows. The most important question is: which rows must remain in the final result?
Follow the preserved side
Suppose customers may have orders, and orders may have shipments. A customer without an order should still appear. An order without a shipment should also still appear. Both relationships are optional, so use LEFT JOIN for both:
WITH shipments (order_id, shipped_on) AS (
VALUES
(101, DATE '2026-11-20'),
(103, DATE '2026-11-21')
)
SELECT
c.name,
o.order_id,
s.shipped_on
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
LEFT JOIN shipments AS s
ON s.order_id = o.order_id
ORDER BY c.customer_id, o.order_id;The joins are evaluated as a chain. First, each customer is matched with orders. Then each resulting row is matched with a shipment. If a customer has no order, the order columns are NULL, and the shipment columns are also NULL. If an order has no shipment, only the shipment columns are NULL.
The preservation rule for this chain is:
| Join step | Rows preserved | Missing columns |
|---|---|---|
| Customers to orders | Every customer | Order columns |
| Orders to shipments | Every row produced by the first join | Shipment columns |
Avoid accidentally creating an inner join
A later inner join can undo the preservation from an earlier outer join:
WITH shipments (order_id, shipped_on) AS (
VALUES
(101, DATE '2026-11-20'),
(103, DATE '2026-11-21')
)
SELECT c.name, o.order_id, s.shipped_on
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
JOIN shipments AS s
ON s.order_id = o.order_id;The JOIN shipments is an inner join. It removes rows where o.order_id is NULL, so customers without orders disappear. It also removes orders without shipments. The whole chain now returns only customers with orders that have shipments.
Use LEFT JOIN for the second relationship when shipments are optional:
...
LEFT JOIN shipments AS s
ON s.order_id = o.order_id;An inner join is appropriate when the later relationship is required by the question. For example, “show customers who have at least one shipped order” should intentionally use an inner join or an existence test.
Keep filters attached to the correct join
A filter in WHERE runs after the joins. It can remove the NULL-padded rows created by a LEFT JOIN:
SELECT c.name, o.order_id
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
LEFT JOIN shipments AS s
ON s.order_id = o.order_id
WHERE s.shipped_on >= DATE '2026-11-20';This keeps only rows with a qualifying shipment. Customers without a shipment are removed because NULL >= DATE '2026-11-20' is not true.
To preserve every customer and order while restricting which shipments match, put the condition in ON:
LEFT JOIN shipments AS s
ON s.order_id = o.order_id
AND s.shipped_on >= DATE '2026-11-20';A useful habit is to read each ON clause as the rule for that relationship, and read WHERE as the final rule for which complete result rows survive.
Exercise
In the two-LEFT-JOIN query, what appears for Cleo if she has no orders, and what appears for order 102 if it has no shipment?
Show answer
Cleo appears once with NULL order and shipment columns. Order 102 appears with its order data and NULL shipment columns.