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;
idnameorder_idstatus
1Somchai Jaidee1paid
1Somchai Jaidee3shipped
1Somchai Jaidee10paid
2Malee Srisuk2shipped
2Malee Srisuk7paid
3Anan Wongsa4paid
3Anan Wongsa14cancelled
4Pim Chaiya5cancelled
4Pim Chaiya12paid
5Narin Thongdee6shipped
6Kanya Boonmee8shipped
6Kanya Boonmee13paid
7Wichai Saelim9paid
7Wichai Saelim15paid
8Siriporn Kaewdee11shipped
9Tawan RungruangNULLNULL
10Lalita MeesukNULLNULL
17 rows

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;
idnamejoined
9Tawan Rungruang2026-05-21
10Lalita Meesuk2026-08-03
2 rows

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;
idnamestock
12Highlighter0
1 row

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;
namewrongorders
Somchai Jaidee33
Malee Srisuk22
Anan Wongsa22
Pim Chaiya22
Narin Thongdee11
Kanya Boonmee22
Wichai Saelim22
Siriporn Kaewdee11
Tawan Rungruang10
Lalita Meesuk10
10 rows

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;
nameorder_idstatus
Somchai Jaidee1paid
Somchai Jaidee10paid
Malee Srisuk7paid
Anan Wongsa4paid
Pim Chaiya12paid
Kanya Boonmee13paid
Wichai Saelim9paid
Wichai Saelim15paid
8 rows

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;
nameorder_idstatus
Somchai Jaidee1paid
Somchai Jaidee10paid
Malee Srisuk7paid
Anan Wongsa4paid
Pim Chaiya12paid
Narin ThongdeeNULLNULL
Kanya Boonmee13paid
Wichai Saelim9paid
Wichai Saelim15paid
Siriporn KaewdeeNULLNULL
Tawan RungruangNULLNULL
Lalita MeesukNULLNULL
12 rows

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;
nameorder_id
Somchai Jaidee1
Somchai Jaidee3
Somchai Jaidee10
Malee Srisuk2
Malee Srisuk7
Anan Wongsa4
Anan Wongsa14
Pim Chaiya5
Pim Chaiya12
Narin Thongdee6
Kanya Boonmee8
Kanya Boonmee13
Wichai Saelim9
Wichai Saelim15
Siriporn Kaewdee11
Tawan RungruangNULL
Lalita MeesukNULL
17 rows

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);
✓ 4 rows changed
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;
namecitydelivery_cityfee
NULLNULLHat Yai60
Somchai JaideeBangkokBangkok30
Malee SrisukChiang MaiChiang Mai50
Anan WongsaBangkokBangkok30
Pim ChaiyaPhuketPhuket60
Narin ThongdeeKhon KaenNULLNULL
Kanya BoonmeeChiang MaiChiang Mai50
Wichai SaelimBangkokBangkok30
Siriporn KaewdeePhuketPhuket60
Tawan RungruangKhon KaenNULLNULL
Lalita MeesukBangkokBangkok30
11 rows

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;
namesame_city_ascity
Somchai JaideeAnan WongsaBangkok
Somchai JaideeWichai SaelimBangkok
Somchai JaideeLalita MeesukBangkok
Anan WongsaWichai SaelimBangkok
Anan WongsaLalita MeesukBangkok
Wichai SaelimLalita MeesukBangkok
Malee SrisukKanya BoonmeeChiang Mai
Narin ThongdeeTawan RungruangKhon Kaen
Pim ChaiyaSiriporn KaewdeePhuket
9 rows

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;
categorycity
DrinksBangkok
DrinksChiang Mai
DrinksHat Yai
DrinksPhuket
BakeryBangkok
BakeryChiang Mai
BakeryHat Yai
BakeryPhuket
SnacksBangkok
SnacksChiang Mai
SnacksHat Yai
SnacksPhuket
StationeryBangkok
StationeryChiang Mai
StationeryHat Yai
StationeryPhuket
16 rows

4 categories × 4 cities = 16 rows. Take care with big tables: two tables of 1,000 rows make a million.

  1. Show every product with how many of it sold altogether (sum(i.qty)), those never sold included, by id.
  2. Which customers have no paid (paid) order at all?
  3. Which cities in delivery have 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;
idnamesold
1Coffee7
2Green Tea5
3Orange Juice2
4Croissant6
5Banana Bread3
6Cheesecake3
7Potato Chips11
8Dried Mango4
9Cashew Nuts2
10Notebook18
11Pen35
12HighlighterNULL
12 rows
idname
5Narin Thongdee
8Siriporn Kaewdee
9Tawan Rungruang
10Lalita Meesuk
4 rows
city
Hat Yai
1 row

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