12. LEFT JOIN and the other joins
Keep the rows that find no partner too, to answer who has never bought and which product doesn't sell.
LEFT JOIN keeps every row of the left table
Chapter 11 showed that JOIN (inner) drops the rows that find no partner: 10 customers, but only 8 left after joining the orders. Some questions want the rows without a partner as well: "every customer, with their orders (if any)".
LEFT JOIN (or LEFT OUTER JOIN) keeps every row of the left table (the one after FROM). When a row finds no partner in the right table, the right table's columns are NULL:
SELECT c.id, c.name, o.id AS order_id, o.status
FROM customer c
LEFT JOIN orders o ON o.customer_id = c.id
ORDER BY c.id, o.id;| id | name | order_id | status |
|---|---|---|---|
| 1 | Somchai Jaidee | 1 | paid |
| 1 | Somchai Jaidee | 3 | shipped |
| 1 | Somchai Jaidee | 10 | paid |
| 2 | Malee Srisuk | 2 | shipped |
| 2 | Malee Srisuk | 7 | paid |
| 3 | Anan Wongsa | 4 | paid |
| 3 | Anan Wongsa | 14 | cancelled |
| 4 | Pim Chaiya | 5 | cancelled |
| 4 | Pim Chaiya | 12 | paid |
| 5 | Narin Thongdee | 6 | shipped |
| 6 | Kanya Boonmee | 8 | shipped |
| 6 | Kanya Boonmee | 13 | paid |
| 7 | Wichai Saelim | 9 | paid |
| 7 | Wichai Saelim | 15 | paid |
| 8 | Siriporn Kaewdee | 11 | shipped |
| 9 | Tawan Rungruang | NULL | NULL |
| 10 | Lalita Meesuk | NULL | NULL |
17 rows: the 15 orders, plus 2 customers who never ordered (Tawan Rungruang and Lalita Meesuk). Their order_id and status are NULL: "no order to go with this".
Left and right are about places in the statement: the table written before LEFT JOIN is the left one, the one after it the right. Swap them and the result changes.
Finding what has no partner: LEFT JOIN ... WHERE ... IS NULL
A row with no partner has NULL in the right table's columns, so those rows can be picked out. The customers who have never ordered:
SELECT c.id, c.name, c.joined
FROM customer c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;| id | name | joined |
|---|---|---|
| 9 | Tawan Rungruang | 2026-05-21 |
| 10 | Lalita Meesuk | 2026-08-03 |
These two joined last (May and August 2026); the shop might send them a coupon to try it.
The products that never sold, the same way with product and order_item:
SELECT p.id, p.name, p.stock
FROM product p
LEFT JOIN order_item i ON i.product_id = p.id
WHERE i.product_id IS NULL;| id | name | stock |
|---|---|---|
| 12 | Highlighter | 0 |
Just one: no one has ever bought a Highlighter.
For the IS NULL test, pick a right-table column that can never be NULL in real data, such as its primary key or the column you join on. Then NULL can only mean "no partner". With a column that is already NULL in some rows, rows that do have a partner slip in too.
Counting to zero: count(column), not count(*)
To count the orders of every customer, those who never ordered included, use LEFT JOIN with GROUP BY, and choose carefully what to count:
SELECT c.name, count(*) AS wrong, count(o.id) AS orders
FROM customer c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name
ORDER BY c.id;| name | wrong | orders |
|---|---|---|
| Somchai Jaidee | 3 | 3 |
| Malee Srisuk | 2 | 2 |
| Anan Wongsa | 2 | 2 |
| Pim Chaiya | 2 | 2 |
| Narin Thongdee | 1 | 1 |
| Kanya Boonmee | 2 | 2 |
| Wichai Saelim | 2 | 2 |
| Siriporn Kaewdee | 1 | 1 |
| Tawan Rungruang | 1 | 0 |
| Lalita Meesuk | 1 | 0 |
For the last two customers, count(*) gives 1, because each has one row (the one with NULLs), while count(o.id) rightly gives 0: count(column) skips NULL, as chapter 9 said.
A condition in ON or in WHERE
With a plain JOIN, a condition gives the same result in ON or in WHERE. With LEFT JOIN it makes a big difference. Say we want "every customer, with their paid orders". Written like this:
SELECT c.name, o.id AS order_id, o.status
FROM customer c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.status = 'paid'
ORDER BY c.id, o.id;| name | order_id | status |
|---|---|---|
| Somchai Jaidee | 1 | paid |
| Somchai Jaidee | 10 | paid |
| Malee Srisuk | 7 | paid |
| Anan Wongsa | 4 | paid |
| Pim Chaiya | 12 | paid |
| Kanya Boonmee | 13 | paid |
| Wichai Saelim | 9 | paid |
| Wichai Saelim | 15 | paid |
we get 8 rows, the 8 paid orders of 6 customers: every customer without a paid order is gone. WHERE works after the join: a row whose o.status is NULL fails o.status = 'paid', and the LEFT JOIN has quietly turned into a plain JOIN.
Move the condition into ON, and it becomes part of finding the partner:
SELECT c.name, o.id AS order_id, o.status
FROM customer c
LEFT JOIN orders o ON o.customer_id = c.id AND o.status = 'paid'
ORDER BY c.id, o.id;| name | order_id | status |
|---|---|---|
| Somchai Jaidee | 1 | paid |
| Somchai Jaidee | 10 | paid |
| Malee Srisuk | 7 | paid |
| Anan Wongsa | 4 | paid |
| Pim Chaiya | 12 | paid |
| Narin Thongdee | NULL | NULL |
| Kanya Boonmee | 13 | paid |
| Wichai Saelim | 9 | paid |
| Wichai Saelim | 15 | paid |
| Siriporn Kaewdee | NULL | NULL |
| Tawan Rungruang | NULL | NULL |
| Lalita Meesuk | NULL | NULL |
12 rows, with all 10 customers: the 8 paid orders, plus the 4 customers without one, who get NULL.
With LEFT JOIN, conditions on the right table go in ON; conditions on the left table (only Bangkok customers, say) go in WHERE. When you see a right-table column in WHERE other than in IS NULL, suspect a mistake.
RIGHT JOIN and FULL JOIN
RIGHT JOIN is LEFT JOIN the other way round: it keeps every row of the right table. This gives the same as customer LEFT JOIN orders above:
SELECT c.name, o.id AS order_id
FROM orders o
RIGHT JOIN customer c ON c.id = o.customer_id
ORDER BY c.id, o.id;| name | order_id |
|---|---|
| Somchai Jaidee | 1 |
| Somchai Jaidee | 3 |
| Somchai Jaidee | 10 |
| Malee Srisuk | 2 |
| Malee Srisuk | 7 |
| Anan Wongsa | 4 |
| Anan Wongsa | 14 |
| Pim Chaiya | 5 |
| Pim Chaiya | 12 |
| Narin Thongdee | 6 |
| Kanya Boonmee | 8 |
| Kanya Boonmee | 13 |
| Wichai Saelim | 9 |
| Wichai Saelim | 15 |
| Siriporn Kaewdee | 11 |
| Tawan Rungruang | NULL |
| Lalita Meesuk | NULL |
Most people write LEFT JOIN with the table to keep whole on the left, which reads more easily, so RIGHT JOIN is rarely used.
FULL JOIN (or FULL OUTER JOIN) keeps every row of both sides. Make a table of delivery fees by city: the shop delivers to 4 cities, which don't match the customers' cities exactly:
CREATE TABLE delivery (city TEXT PRIMARY KEY, fee INTEGER);
INSERT INTO delivery VALUES ('Bangkok', 30), ('Chiang Mai', 50), ('Phuket', 60), ('Hat Yai', 60);SELECT c.name, c.city, d.city AS delivery_city, d.fee
FROM customer c
FULL JOIN delivery d ON d.city = c.city
ORDER BY c.id;| name | city | delivery_city | fee |
|---|---|---|---|
| NULL | NULL | Hat Yai | 60 |
| Somchai Jaidee | Bangkok | Bangkok | 30 |
| Malee Srisuk | Chiang Mai | Chiang Mai | 50 |
| Anan Wongsa | Bangkok | Bangkok | 30 |
| Pim Chaiya | Phuket | Phuket | 60 |
| Narin Thongdee | Khon Kaen | NULL | NULL |
| Kanya Boonmee | Chiang Mai | Chiang Mai | 50 |
| Wichai Saelim | Bangkok | Bangkok | 30 |
| Siriporn Kaewdee | Phuket | Phuket | 60 |
| Tawan Rungruang | Khon Kaen | NULL | NULL |
| Lalita Meesuk | Bangkok | Bangkok | 30 |
11 rows. The two customers in Khon Kaen have no fee (the shop doesn't deliver there yet), and the first row, Hat Yai, has no customer (the shop delivers there, but no one lives there yet). NULL sorts before every other value, so that row comes first.
SQLite has taken RIGHT JOIN and FULL JOIN since version 3.39 (2022), and Phai SQL does both. An older SQLite gives an error.
Self join: a table joined to itself
A table can be joined to itself: just give it two short names to tell the sides apart. Pairs of customers in the same city (they might order together and get one delivery):
SELECT a.name, b.name AS same_city_as, a.city
FROM customer a
JOIN customer b ON b.city = a.city AND a.id < b.id
ORDER BY a.city, a.id, b.id;| name | same_city_as | city |
|---|---|---|
| Somchai Jaidee | Anan Wongsa | Bangkok |
| Somchai Jaidee | Wichai Saelim | Bangkok |
| Somchai Jaidee | Lalita Meesuk | Bangkok |
| Anan Wongsa | Wichai Saelim | Bangkok |
| Anan Wongsa | Lalita Meesuk | Bangkok |
| Wichai Saelim | Lalita Meesuk | Bangkok |
| Malee Srisuk | Kanya Boonmee | Chiang Mai |
| Narin Thongdee | Tawan Rungruang | Khon Kaen |
| Pim Chaiya | Siriporn Kaewdee | Phuket |
a.id < b.id makes each pair come out once and never pairs someone with themselves. 9 pairs: Bangkok's 4 customers make 6 pairs, the other three cities 1 pair each.
CROSS JOIN: every row with every row
CROSS JOIN pairs every row on the left with every row on the right, with no ON. It is useful when you want "every combination": an empty grid for sales targets, say, every category in every city the shop delivers to:
SELECT cat.name AS category, d.city
FROM category cat
CROSS JOIN delivery d
ORDER BY cat.id, d.city;| category | city |
|---|---|
| Drinks | Bangkok |
| Drinks | Chiang Mai |
| Drinks | Hat Yai |
| Drinks | Phuket |
| Bakery | Bangkok |
| Bakery | Chiang Mai |
| Bakery | Hat Yai |
| Bakery | Phuket |
| Snacks | Bangkok |
| Snacks | Chiang Mai |
| Snacks | Hat Yai |
| Snacks | Phuket |
| Stationery | Bangkok |
| Stationery | Chiang Mai |
| Stationery | Hat Yai |
| Stationery | Phuket |
4 categories × 4 cities = 16 rows. Take care with big tables: two tables of 1,000 rows make a million.
- Show every product with how many of it sold altogether (
sum(i.qty)), those never sold included, byid. - Which customers have no paid (
paid) order at all? - Which cities in
deliveryhave no customer yet?
Show the answer
SELECT p.id, p.name, sum(i.qty) AS sold FROM product p LEFT JOIN order_item i ON i.product_id = p.id GROUP BY p.id, p.name ORDER BY p.id;
SELECT c.id, c.name FROM customer c LEFT JOIN orders o ON o.customer_id = c.id AND o.status = 'paid' WHERE o.id IS NULL ORDER BY c.id;
SELECT d.city FROM delivery d LEFT JOIN customer c ON c.city = d.city WHERE c.id IS NULL;| id | name | sold |
|---|---|---|
| 1 | Coffee | 7 |
| 2 | Green Tea | 5 |
| 3 | Orange Juice | 2 |
| 4 | Croissant | 6 |
| 5 | Banana Bread | 3 |
| 6 | Cheesecake | 3 |
| 7 | Potato Chips | 11 |
| 8 | Dried Mango | 4 |
| 9 | Cashew Nuts | 2 |
| 10 | Notebook | 18 |
| 11 | Pen | 35 |
| 12 | Highlighter | NULL |
| id | name |
|---|---|
| 5 | Narin Thongdee |
| 8 | Siriporn Kaewdee |
| 9 | Tawan Rungruang |
| 10 | Lalita Meesuk |
| city |
|---|
| Hat Yai |
Phai (bamboo), given as a gift of knowledge: free to use, learn from and pass on · every example on this page was run on Phai SQL and checked against SQLite when the page was made