SQL LEFT JOIN
LEFT JOIN (also LEFT OUTER JOIN) returns every row of the left table, together with the matching rows of the right table. Where there is no match, the right table's columns are NULL.
SELECT columns
FROM left_table
LEFT JOIN right_table ON left_table.column = right_table.column;All customers and their orders
SELECT c.customer_id, c.name, o.order_id
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
WHERE c.customer_id BETWEEN 7 AND 13
ORDER BY c.customer_id, o.order_id;customer_id | name | order_id
------------+-----------------+---------
7 | Green Fork | 14
7 | Green Fork | 16
8 | Night Owl Books | NULL
9 | Tech Nest | 33
10 | Daily Bread | NULL
11 | Yuki Tanaka | 23
12 | Omar Murphy | 12
13 | Priya Kowalski | NULL
(8 rows)
Customers 8, 10 and 13 have no orders, yet they appear — with NULL as order_id.
Counting, including zeros
A classic use: orders per customer, including customers with zero orders. Count a column from the right table, because COUNT(*) would count the NULL row as 1:
SELECT c.name,
COUNT(o.order_id) AS orders,
COUNT(*) AS wrong_count
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
WHERE c.country IN ('USA', 'Germany', 'Sweden')
GROUP BY c.customer_id
ORDER BY orders DESC, c.name;name | orders | wrong_count
----------------+--------+------------
Corner Cafe | 1 | 1
Tech Nest | 1 | 1
Hugo Berg | 0 | 1
Night Owl Books | 0 | 1
Priya Kowalski | 0 | 1
(5 rows)
Finding rows without a match
LEFT JOIN plus WHERE right.key IS NULL finds left rows with no partner — an "anti-join". Products that were never ordered:
SELECT p.product_id, p.name
FROM products AS p
LEFT JOIN order_items AS oi ON oi.product_id = p.product_id
WHERE oi.item_id IS NULL
ORDER BY p.product_id;product_id | name
-----------+---------------
14 | Avocados
27 | Coffee Grinder
(2 rows)
ON vs WHERE in outer joins
With a LEFT JOIN, a condition on the right table behaves differently in ON and in WHERE:
- In ON it limits which right rows are matched; left rows are still kept.
- In WHERE it filters the final rows — and removes the NULL rows, turning the query into an inner join.
-- Delivered orders per customer, keeping customers with none
SELECT c.name, COUNT(o.order_id) AS delivered
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id AND o.status = 'delivered'
WHERE c.customer_id <= 6
GROUP BY c.customer_id
ORDER BY c.customer_id;
-- Same condition in WHERE: customers without delivered orders vanish
SELECT c.name, COUNT(o.order_id) AS delivered
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
WHERE c.customer_id <= 6 AND o.status = 'delivered'
GROUP BY c.customer_id
ORDER BY c.customer_id;name | delivered
----------------+----------
Corner Cafe | 1
Bright Bytes | 2
Nonna's Kitchen | 0
Pixel Studio | 0
Le Petit Marche | 1
Code & Coffee | 1
(6 rows)
name | delivered
----------------+----------
Corner Cafe | 1
Bright Bytes | 2
Le Petit Marche | 1
Code & Coffee | 1
(4 rows)
Tip: Put conditions about the right table into
ONwhen you want to keep unmatched left rows. Put conditions about the left table intoWHERE.
Test yourself
In customers LEFT JOIN orders, what happens to a customer without orders?
- It is dropped
- It appears once with NULL order columns
- It appears with order_id = 0
- The query fails
Which pattern finds products that were never ordered?
- INNER JOIN ... WHERE oi.item_id IS NULL
- LEFT JOIN order_items oi ... WHERE oi.item_id IS NULL
- LEFT JOIN ... WHERE oi.item_id IS NOT NULL
- CROSS JOIN order_items
Why use COUNT(o.order_id) instead of COUNT(*) after a LEFT JOIN?
- It is faster
- COUNT(*) counts the NULL-filled row as 1 for customers without orders
- COUNT(*) is not allowed
- No difference