Sub topic 3
LEFT OUTER JOIN
Prerequisites
This chapter assumes you can write SELECT, FROM, and WHERE queries. You should also know primary keys, foreign keys, NULL, and INNER JOIN with an ON condition.
An outer join is useful when you need rows even when the other table has no match. A LEFT OUTER JOIN preserves every row from the table on the left side of the join. Matching rows from the right table are added when they exist. If no match exists, SQL fills the right-table columns with NULL.
LEFT JOIN and LEFT OUTER JOIN mean the same thing. OUTER is optional.
The sample tables
Use these tables throughout the examples:
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,
FOREIGN KEY (customer_id) REFERENCES customers (customer_id)
);
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);Ada has two orders, Ben has one, and Cleo has none. Order 104 has no customer.
How LEFT JOIN preserves rows
Start with customers on the left:
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 is:
| name | order_id | total |
|---|---|---|
| Ada | 101 | 50.00 |
| Ada | 102 | 20.00 |
| Ben | 103 | 35.00 |
| Cleo | NULL | NULL |
Cleo appears even though no row in orders matches her customer_id. The NULL values are placeholders for columns from the unmatched right-side row.
An inner join would discard Cleo because it returns only rows with a match. A left join instead answers this question:
“Show every customer, along with any orders that customer has.”
A row can appear more than once when several right-side rows match one left-side row. Ada appears twice because she has two orders. This repetition is expected. The join returns matching pairs, not one summary row per customer.
Reading the NULL values
To find customers with no orders, test a right-table column that cannot be NULL for a real order, such as the primary key:
SELECT c.customer_id, 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 Cleo. Use IS NULL, not = NULL, because NULL means “unknown” rather than a regular value.
For example, this query removes Cleo:
SELECT c.name, o.order_id
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
WHERE o.total > 30;For Cleo, o.total is NULL, so o.total > 30 is not true. The WHERE clause discards that row. The query therefore behaves like an inner join for this condition.
If the condition belongs to the matching rule, place it in ON instead:
SELECT c.name, o.order_id
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
AND o.total > 30;This keeps every customer but attaches only orders greater than 30. Cleo remains with a NULL order.
Exercise
Which customers appear in 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, and why?
Show answer
Only Cleo appears. The left join preserves her, and o.order_id IS NULL identifies the missing right-side match.