Sub topic 15
When an inner join is not enough: outer joins
This chapter assumes you can write an INNER JOIN with an ON condition and table aliases. It also assumes you know that NULL never equals anything, including another NULL. The examples use the customers and orders tables from earlier chapters.
Why inner joins lose rows
An inner join keeps only the pairs of rows that satisfy the ON condition. That is correct for questions such as "which orders belong to which customers?". It is the wrong tool for questions that are about rows that may have no partner:
- Which customers have never placed an order?
- List every customer with their order count, including customers with zero orders.
- Which orders are not linked to any customer?
With our sample data, the inner join returns three rows. Chloe (no orders) and order 104 (no customer) are dropped. An outer join keeps those unmatched rows and fills the missing side with NULL.
The three outer joins
Every outer join starts like an inner join. It first finds all matching pairs. It then adds the unmatched rows from the side or sides you chose to preserve, with NULL in every column that comes from the other table.
| Join type | Unmatched rows kept from | Columns filled with NULL |
|---|---|---|
| `INNER JOIN` | neither table | none |
| `LEFT OUTER JOIN` | the table written before the keyword (left) | right table's columns |
| `RIGHT OUTER JOIN` | the table written after the keyword (right) | left table's columns |
| `FULL OUTER JOIN` | both tables | whichever side has no match |
The word OUTER is optional. LEFT JOIN and LEFT OUTER JOIN mean the same thing.
LEFT JOIN in practice
Keep every customer, whether or not they have orders:
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;The result has four rows:
| name | order_id | total |
|---|---|---|
| Ada | 101 | 50.00 |
| Ada | 102 | 75.00 |
| Ben | 103 | 20.00 |
| Chloe | NULL | NULL |
Order 104 is still absent, because it belongs to the right table and a left join only preserves the left side.
A RIGHT JOIN is the mirror image. Swapping the table order turns one into the other, so most people write only LEFT JOIN and arrange the tables to suit. This keeps queries easier to read from top to bottom.
A FULL JOIN keeps both sides:
SELECT c.name, o.id AS order_id, o.total
FROM customers AS c
FULL OUTER JOIN orders AS o
ON o.customer_id = c.id;This returns five rows: the three matches, Chloe with NULL order columns, and order 104 with NULL in name. PostgreSQL, SQL Server, Oracle and SQLite (3.39 and later) support FULL OUTER JOIN. MySQL and MariaDB do not.
Finding rows with no match (anti-join)
A common use of a left join is to find rows that have no partner. Join, then keep only the rows where the right side is NULL:
SELECT c.id, c.name
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.id
WHERE o.id IS NULL;This returns Chloe. Test a column that can never be NULL in real matched rows, such as the primary key o.id. If you test a nullable column such as o.customer_id or a nullable o.total, a genuinely matched row with a NULL value would be mistaken for "no match".
The same question can be written with NOT EXISTS:
SELECT c.id, c.name
FROM customers AS c
WHERE NOT EXISTS (
SELECT 1
FROM orders AS o
WHERE o.customer_id = c.id
);Both forms give the same answer here. Many optimizers treat them alike, so choose the one you find clearer. Avoid NOT IN (SELECT customer_id FROM orders) for this job. Order 104 has a NULL customer_id, so the subquery returns a NULL. Then c.id NOT IN (...) can never be true, and the query returns no rows at all.
WHERE versus ON in an outer join
In an inner join, a filter in WHERE and the same filter in ON give the same result. In an outer join they do not.
ONdecides which rows match. Rows on the preserved side that fail it are still kept, withNULLon the other side.WHEREruns after the join and removes finished rows. A row withNULLin the filtered column fails the test and disappears.
Suppose you want all customers, with only their orders over 30:
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;Ada keeps 101 and 102. Ben's order 103 (total 20) fails the condition, so Ben appears once with NULL order columns. Chloe also appears with NULLs. Three customers are in the result.
Now move the condition to WHERE:
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
WHERE o.total > 30;Ben's order 103 is joined, then removed by the filter. Chloe's NULL total fails NULL > 30, so she is removed too. Only Ada's two rows remain. The query still says LEFT JOIN, but it behaves like an inner join. This is one of the most common outer join bugs.
Counting with outer joins
Use a column from the optional side when counting. COUNT(*) counts rows, and a customer with no orders still produces one row:
SELECT c.name,
COUNT(*) AS wrong_count,
COUNT(o.id) AS order_count,
COALESCE(SUM(o.total), 0) AS revenue
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.id
GROUP BY c.id, c.name;For Chloe, COUNT(*) returns 1 but COUNT(o.id) returns 0, because COUNT(column) skips NULLs. SUM over no non-null values returns NULL, so COALESCE supplies a 0.
Chains of outer joins
When you chain joins, a later INNER JOIN on the optional table discards the preserved rows again. Suppose you add order_items after the left join:
SELECT c.name, o.id AS order_id, i.product_id
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.id
INNER JOIN order_items AS i ON i.order_id = o.id;Chloe's row has o.id equal to NULL, so it cannot match any item and the inner join removes her. Use LEFT JOIN order_items as well when you want to keep customers without orders.
Choosing a join type
- Start with the question. Do you need only rows that have a partner? Use
INNER JOIN. - Do you need every row of one table whatever happens in the other? Make that table the preserved side of a
LEFT JOIN. - Do you need to find the rows with no partner? Use
LEFT JOIN ... WHERE right.pk IS NULLorNOT EXISTS. - Do you need unmatched rows from both sides, such as when reconciling two data sources? Use
FULL OUTER JOIN. - Keep outer joins to the cases that need them. Where an inner join answers the question, it states the intent more clearly.
Exercise
Using the sample data, write a query that lists every customer with the number of orders over 30 they have placed, showing 0 for customers with none. Which customers appear, and with what counts?
Show answer
SELECT c.name, COUNT(o.id) AS bigorders FROM customers AS c LEFT JOIN orders AS o ON o.customerid = c.id AND o.total > 30 GROUP BY c.id, c.name; The filter goes in ON so customers without a qualifying order are kept. COUNT(o.id) ignores NULLs. Result: Ada 2, Ben 0, Chloe 0. Putting o.total > 30 in WHERE would drop Ben and Chloe.
Exercise
A query uses LEFT JOIN orders and then WHERE o.order_date >= '2026-02-01'. Chloe is missing from the result. Why, and how do you keep her?
Show answer
Chloe has no orders, so o.orderdate is NULL for her row. NULL >= '2026-02-01' is not true, so WHERE removes the row. Move the date test into the ON clause (ON o.customerid = c.id AND o.order_date >= '2026-02-01') so the left join keeps Chloe with NULL order columns.