Sub topic 2
The Idea Behind Outer Joins: Preserving Rows
This chapter assumes you know SELECT, WHERE, primary keys, foreign keys, NULL, and INNER JOIN with an ON condition.
An outer join answers a different question from an inner join:
- An inner join returns only rows that have a match on both sides.
- An outer join also returns selected rows that have no match.
The important idea is preservation. An outer join preserves rows from one table, or from both tables, even when the join condition finds nothing.
What an inner join drops
Suppose you have customers and orders:
| customer_id | name | order_id | total |
|---|---|---|---|
| 1 | Ada | 101 | 50.00 |
| 1 | Ada | 102 | 20.00 |
| 2 | Ben | 103 | 35.00 |
| 3 | Cleo | NULL | NULL |
| NULL | NULL | 104 | 15.00 |
Cleo is a customer with no order. Order 104 has no customer. An inner join drops both unmatched situations:
SELECT c.name, o.order_id, o.total
FROM customers AS c
INNER JOIN orders AS o
ON o.customer_id = c.customer_id;The result contains Ada's and Ben's orders only. Cleo disappears because no order matches her. Order 104 disappears because no customer matches it.
The preserved side
Each outer join specifies which rows must survive:
| Join type | Rows preserved |
|---|---|
| `LEFT OUTER JOIN` | Every row from the table on the left |
| `RIGHT OUTER JOIN` | Every row from the table on the right |
| `FULL OUTER JOIN` | Every row from both tables |
OUTER is optional. These are equivalent:
LEFT JOIN
LEFT OUTER JOINThink of the join as having two sides:
flowchart TD
A[Customers table] --> B{Join condition}
C[Orders table] --> B
B --> D[Matching rows are combined]
B --> E[Unmatched preserved rows survive]
E --> F[Missing side is filled with NULL]The table named immediately after FROM is the left side. The table named after JOIN is the right side.
NULL padding
When a preserved row has no match, SQL creates one result row and fills the columns from the missing side with NULL. This is called NULL padding.
For example:
SELECT c.name, o.order_id, o.total
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id;The result includes:
Ada 101 50.00
Ada 102 20.00
Ben 103 35.00
Cleo NULL NULLCleo's row exists because LEFT JOIN preserves every customer. The NULL values do not mean that Cleo has an order with unknown details. They mean that no matching order row exists.
This distinction matters when you filter or aggregate the result. To test for a missing match, use IS NULL, not = NULL:
SELECT c.name
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;This returns customers with no order.
> callout note > NULL padding applies to columns from the unmatched side. Columns from the preserved table still contain their stored values.
Choosing the join type
Start by stating which rows your result must include:
- “Show every customer, including customers with no orders.” Use
LEFT JOINwith customers on the left. - “Show every order, including orders with no customer.” Use
LEFT JOINwith orders on the left, or aRIGHT JOINwith orders on the right. - “Show every customer and every order, whether matched or unmatched.” Use
FULL OUTER JOIN.
A RIGHT JOIN is usually interchangeable with a LEFT JOIN by reversing the table order. Many teams prefer LEFT JOIN because it makes the preserved table easier to see:
-- RIGHT JOIN
SELECT c.name, o.order_id
FROM customers AS c
RIGHT JOIN orders AS o
ON o.customer_id = c.customer_id;
-- Equivalent LEFT JOIN
SELECT c.name, o.order_id
FROM orders AS o
LEFT JOIN customers AS c
ON c.customer_id = o.customer_id;The two queries preserve every order and return the same matching relationships.
Exercise
Which join should you use to list every product, including products that have never been sold?
Show answer
Put products on the left and use LEFT JOIN: SELECT p.name, s.saleid FROM products AS p LEFT JOIN sales AS s ON s.productid = p.product_id;