Sub topic 8
FULL OUTER JOIN
Prerequisites and the goal
This chapter assumes that you can write SELECT, FROM, and WHERE queries, understand primary and foreign keys, know what NULL means, and have used INNER JOIN.
A FULL OUTER JOIN returns every row from both tables. When rows match, SQL combines them. When a row has no match, SQL fills the other table's columns with NULL.
This differs from an inner join, which discards unmatched rows.
A full join with sample data
Suppose these tables contain customers and orders:
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL
);
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
total DECIMAL(10, 2) NOT NULL
);
INSERT INTO customers (customer_id, name) VALUES
(1, 'Ada'), (2, 'Ben'), (3, 'Cleo');
INSERT INTO orders (order_id, customer_id, total) VALUES
(101, 1, 50.00),
(102, 1, 20.00),
(103, 2, 35.00),
(104, NULL, 15.00);A full join shows matches and unmatched rows from both sides:
SELECT c.name, o.order_id, o.total
FROM customers AS c
FULL OUTER JOIN orders AS o
ON o.customer_id = c.customer_id;One possible result is:
| name | order_id | total |
|---|---|---|
| Ada | 101 | 50.00 |
| Ada | 102 | 20.00 |
| Ben | 103 | 35.00 |
| Cleo | NULL | NULL |
| NULL | 104 | 15.00 |
Cleo is preserved because she has no order. Order 104 is preserved because its customer_id is NULL, so it matches no customer. The database can return these rows in a different order unless you add ORDER BY.
Reading the preserved sides
The word FULL means both inputs are preserved. Compare the join types:
| Join | Preserved rows |
|---|---|
| `INNER JOIN` | Only rows with a match |
| `LEFT JOIN` | Every row from the left table |
| `RIGHT JOIN` | Every row from the right table |
| `FULL OUTER JOIN` | Every row from both tables |
The table position matters. In the query above, customers is on the left and orders is on the right. For a full join, switching the tables does not change which rows are preserved, although the selected columns and their order may change.
Filtering full-join results
A condition in WHERE runs after the join. It can remove the NULL-padded rows:
SELECT c.name, o.order_id
FROM customers AS c
FULL OUTER JOIN orders AS o
ON o.customer_id = c.customer_id
WHERE o.total > 30;This removes Cleo because NULL > 30 is not true. It also removes order 104 because its customer columns are NULL. The query now returns only matched orders above 30.
To keep unmatched rows while filtering matched orders, include the unmatched case explicitly:
SELECT c.name, o.order_id, o.total
FROM customers AS c
FULL OUTER JOIN orders AS o
ON o.customer_id = c.customer_id
WHERE o.total > 30
OR o.order_id IS NULL
OR c.customer_id IS NULL;For conditions that determine which rows can match, placing the condition in ON often makes the intent clearer. Always check which side must remain visible.
> callout note > NULL is not an empty string or zero. Use IS NULL or IS NOT NULL to test it. Comparisons such as o.total = NULL never produce true.
Duplicate matches
A full join does not force one result row per input row. If one customer has three matching orders, that customer appears three times. If both sides contain duplicate values for the join key, SQL produces every matching pair.
Use GROUP BY when you need one summary row per customer. Use ORDER BY when you need predictable display order.
Database support
PostgreSQL, SQL Server, and Oracle support FULL OUTER JOIN. MySQL does not provide it directly. On MySQL, combine a left join with right-side-only rows:
SELECT c.name, o.order_id
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
UNION ALL
SELECT c.name, o.order_id
FROM customers AS c
RIGHT JOIN orders AS o
ON o.customer_id = c.customer_id
WHERE c.customer_id IS NULL;The second query keeps only orders that had no customer in the first query. UNION ALL then adds those rows without repeating matches.
Exercise
Which row represents an order with no matching customer in the full-join example, and which columns are NULL?
Show answer
Order 104 represents the unmatched order. Its customer columns, including c.name and c.customer_id if selected, are NULL.