Sub topic 15
When an Inner Join Is Not Enough: Outer Joins
Assume you know basic SELECT, FROM, WHERE, and ORDER BY syntax. You should also understand primary keys, foreign keys, and how an INNER JOIN keeps only rows that match on both sides.
An inner join answers this question: “Which rows have a related row in both tables?” Sometimes you need a different question: “Show every customer, including customers who have not placed an order.” An outer join keeps unmatched rows from one or both tables.
Choosing an outer join
A LEFT JOIN keeps every row from the table on the left side of the join. If no matching row exists on the right, the right-side columns contain NULL.
A RIGHT JOIN does the same for the table on the right. You can usually rewrite it as a LEFT JOIN by switching the table order, so LEFT JOIN is more common.
A FULL OUTER JOIN keeps every row from both tables. Where no match exists, the columns from the missing side contain NULL. PostgreSQL supports it, but some database systems, including MySQL, do not provide it directly.
| Join type | Rows preserved |
|---|---|
| `INNER JOIN` | Only matching rows |
| `LEFT JOIN` | Every left-side row |
| `RIGHT JOIN` | Every right-side row |
| `FULL OUTER JOIN` | Every row from both sides |
Finding customers with and without orders
With the sample customers and orders tables, this query lists every customer:
SELECT
c.name,
o.id AS order_id,
o.total
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.id
ORDER BY c.id, o.id;Ada appears twice because she has two orders. Ben appears once. Chloe appears once with NULL for order_id and total, because she has no order.
The join condition still decides which rows match. The word LEFT changes what happens to rows that do not match.
Filtering without removing preserved rows
A common mistake is putting a right-side filter in WHERE:
SELECT c.name, o.id AS order_id
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.id
WHERE o.total > 30;For Chloe, o.total is NULL. The condition NULL > 30 is not true, so WHERE removes Chloe. The query now behaves like an inner join for this filter.
Put the condition in ON when you want to preserve customers whose orders do not meet the condition:
SELECT c.name, o.id AS order_id, o.total
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.id
AND o.total > 30
ORDER BY c.id, o.id;This returns every customer. Orders costing 30 or less fail the match, but the customer remains with NULL order columns.
When to use each type
Choose an inner join when unmatched rows have no meaning for the question. Choose a left join when the left table is your complete list and related data is optional. Use a full outer join when you need to compare two lists and see missing entries on either side.
Exercise
How would you list every order, including order 104, and show the customer name when a customer exists?
Show answer
Start with orders on the left: SELECT o.id, c.name FROM orders AS o LEFT JOIN customers AS c ON c.id = o.customer_id;