SQL LEFT JOIN
What you will learn
- How
LEFT JOINkeeps every row of the left table - How unmatched rows show
NULLon the right - How to count children including zero
- Why a condition on the right table goes in
ON, notWHERE
This lesson runs on the sample database you chose in lesson 1.1 (use the Example data picker at the top). Edit any query and press Run. Reset data puts the original rows back.
1. The problem with INNER JOIN
An inner join drops the parent rows that have no child. Sometimes those rows are exactly what you need: customers who have never ordered, courses with no sections. A LEFT JOIN keeps every row of the left table (the one before LEFT JOIN), and fills the right side with NULL where there is no match:
SELECT p.customer_id, p.first_name, c.order_id
FROM customers AS p
LEFT JOIN orders AS c ON c.customer_id = p.customer_id
ORDER BY p.customer_id, c.order_id LIMIT 6;
customer_id first_name order_id
----------- ---------- --------
1 Vik 135
1 Vik 289
1 Vik 328
1 Vik 554
1 Vik 952
1 Vik 1070
(6 rows)
SELECT p.customer_id, p.full_name, c.account_id
FROM customers AS p
LEFT JOIN accounts AS c ON c.customer_id = p.customer_id
ORDER BY p.customer_id, c.account_id LIMIT 6;
customer_id full_name account_id
----------- ----------- ----------
1 Chen Park 316
2 Rafi Uddin NULL
3 Grace Kamau 101
3 Grace Kamau 278
4 Mei Bianchi NULL
5 Farid Lee 41
(6 rows)
SELECT p.course_id, p.code, c.section_id
FROM courses AS p
LEFT JOIN sections AS c ON c.course_id = p.course_id
ORDER BY p.course_id, c.section_id LIMIT 6;
course_id code section_id
--------- ------- ----------
1 MATH101 1
2 MATH201 2
2 MATH201 3
2 MATH201 4
3 MATH301 5
3 MATH301 6
(6 rows)
Rows with a child show its id. Rows with no child show NULL in the child's columns, if the data has any (the first rows may all have children; the next section finds those that do not).
2. How many parents have no child?
Compare the sizes of the inner and the left join (lesson 4.2):
SELECT (SELECT COUNT(*) FROM customers) AS parents,
(SELECT COUNT(DISTINCT p.customer_id) FROM customers AS p JOIN orders AS c ON c.customer_id = p.customer_id) AS parents_with_children,
(SELECT COUNT(*) FROM customers AS p LEFT JOIN orders AS c ON c.customer_id = p.customer_id WHERE c.customer_id IS NULL) AS parents_without_children;
parents parents_with_children parents_without_children
------- --------------------- ------------------------
420 365 55
(1 row)
SELECT (SELECT COUNT(*) FROM customers) AS parents,
(SELECT COUNT(DISTINCT p.customer_id) FROM customers AS p JOIN accounts AS c ON c.customer_id = p.customer_id) AS parents_with_children,
(SELECT COUNT(*) FROM customers AS p LEFT JOIN accounts AS c ON c.customer_id = p.customer_id WHERE c.customer_id IS NULL) AS parents_without_children;
parents parents_with_children parents_without_children
------- --------------------- ------------------------
300 236 64
(1 row)
SELECT (SELECT COUNT(*) FROM courses) AS parents,
(SELECT COUNT(DISTINCT p.course_id) FROM courses AS p JOIN sections AS c ON c.course_id = p.course_id) AS parents_with_children,
(SELECT COUNT(*) FROM courses AS p LEFT JOIN sections AS c ON c.course_id = p.course_id WHERE c.course_id IS NULL) AS parents_without_children;
parents parents_with_children parents_without_children
------- --------------------- ------------------------
19 18 1
(1 row)
The last two numbers add up to the first. (The next lesson uses the IS NULL test to list the lonely parents.)
3. Counting children, including zero
With an inner join, parents without children vanish from a count. With a LEFT JOIN, count a column of the child so that unmatched rows count as zero:
SELECT c.customer_id, c.first_name, c.last_name, COUNT(o.order_id) AS orders
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
GROUP BY c.customer_id, c.first_name, c.last_name
ORDER BY orders, c.customer_id LIMIT 6;
customer_id first_name last_name orders
----------- ---------- --------- ------
14 Leo Khan 0
19 Dana Sato 0
22 Jin Lopez 0
23 Bruno Wang 0
25 Tariq Uddin 0
29 Wei Kamau 0
(6 rows)
SELECT c.customer_id, c.full_name, COUNT(a.account_id) AS accounts
FROM customers AS c
LEFT JOIN accounts AS a ON a.customer_id = c.customer_id
GROUP BY c.customer_id, c.full_name
ORDER BY accounts, c.customer_id LIMIT 6;
customer_id full_name accounts
----------- ----------- --------
2 Rafi Uddin 0
4 Mei Bianchi 0
6 Farid Park 0
14 Leo Rahman 0
18 Ines Cohen 0
21 Sven Varga 0
(6 rows)
SELECT c.course_id, c.code, c.title, COUNT(s.section_id) AS sections
FROM courses AS c
LEFT JOIN sections AS s ON s.course_id = c.course_id
GROUP BY c.course_id, c.code, c.title
ORDER BY sections, c.course_id LIMIT 6;
course_id code title sections
--------- ------- ---------------------- --------
17 CS101 Computing Basics 0
1 MATH101 Foundations of Algebra 1
4 SCI101 General Science 1
19 CS301 Databases 1
3 MATH301 Statistics 2
5 SCI201 Chemistry 2
(6 rows)
Count a column of the child (COUNT(o.order_id)), not COUNT(*). COUNT(*) counts the row itself, so a parent with no child would show 1. COUNT(column) skips the NULL (lesson 3.4) and correctly gives 0.
4. ON or WHERE for the right table?
This is the most important detail of a LEFT JOIN. A condition on the right table behaves differently in ON and in WHERE:
- In
ON, it decides which children match; parents with no matching child are still kept, withNULL. - In
WHERE, it runs after the join and removes every row where the right side isNULL, which turns the left join back into an inner join.
Compare the two on the same condition:
Customers and only their cancelled orders:
SELECT COUNT(*) AS rows_with_condition_in_on
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id AND o.status = 'cancelled';
rows_with_condition_in_on
-------------------------
457
(1 row)
SELECT COUNT(*) AS rows_with_condition_in_where
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
WHERE o.status = 'cancelled';
rows_with_condition_in_where
----------------------------
170
(1 row)
Customers and only their savings accounts:
SELECT COUNT(*) AS rows_with_condition_in_on
FROM customers AS c
LEFT JOIN accounts AS a ON a.customer_id = c.customer_id AND a.account_type = 'savings';
rows_with_condition_in_on
-------------------------
326
(1 row)
SELECT COUNT(*) AS rows_with_condition_in_where
FROM customers AS c
LEFT JOIN accounts AS a ON a.customer_id = c.customer_id
WHERE a.account_type = 'savings';
rows_with_condition_in_where
----------------------------
134
(1 row)
Courses and only their 2025-Spring sections:
SELECT COUNT(*) AS rows_with_condition_in_on
FROM courses AS c
LEFT JOIN sections AS s ON s.course_id = c.course_id AND s.term = '2025-Spring';
rows_with_condition_in_on
-------------------------
19
(1 row)
SELECT COUNT(*) AS rows_with_condition_in_where
FROM courses AS c
LEFT JOIN sections AS s ON s.course_id = c.course_id
WHERE s.term = '2025-Spring';
rows_with_condition_in_where
----------------------------
16
(1 row)
The first count includes every parent (those without a matching child appear once, with NULL). The second includes only parents that have a matching child. Use ON when you mean "match these children if there are any", and WHERE when you mean "keep only the rows that match".
5. RIGHT and FULL joins
RIGHT JOIN keeps every row of the right table, and FULL JOIN keeps every row of both. You can always rewrite a RIGHT JOIN by swapping the tables and using LEFT JOIN, so most people only use LEFT JOIN. SQLite supports RIGHT and FULL joins from version 3.39; older versions do not, which is another reason to prefer LEFT JOIN.
Worked example: choose the join
| Question | Join |
|---|---|
| "each order with its customer" (every order has a customer) | JOIN (inner) |
| "every customer and how many orders they placed, even zero" | LEFT JOIN from customers |
| "customers who never ordered" | LEFT JOIN and keep the rows where the order is NULL (lesson 4.4) |
| "every customer and only their cancelled orders, if any" | LEFT JOIN with the condition in ON |
The tables are made up.
Common mistakes
COUNT(*)after aLEFT JOIN. It counts the unmatched row as 1. Count a column of the right table.- A right-table condition in
WHERE, which cancels the left join. - Putting the tables the wrong way round. The table you want to keep complete comes before
LEFT JOIN. - Using
RIGHT JOINwhen swapping the tables and usingLEFT JOINis clearer.
Key takeaways
LEFT JOINkeeps every row of the left table; unmatched rows showNULLon the right.- Count a column of the right table to get zeros:
COUNT(c.id). - A condition on the right table goes in
ONto keep unmatched rows; inWHEREit removes them. - Prefer
LEFT JOINtoRIGHT JOIN.
Practice
1. Which table's rows are all kept in A LEFT JOIN B?
Show answer
Table A, the left one.
2. What do the columns of B show for a row of A that has no match?
Show answer
NULL.
3. Why does COUNT(*) give 1 for a parent without children after a LEFT JOIN?
Show answer
The parent still appears as one row, and COUNT(*) counts rows. COUNT(child.id) skips the NULL and gives 0.
4. What happens if you put WHERE b.status = 'x' after a LEFT JOIN b?
Show answer
Rows where b has no match (NULL) are removed, so the left join behaves like an inner join.
5. How do you rewrite A RIGHT JOIN B?
Show answer
B LEFT JOIN A: swap the tables and use LEFT JOIN.
6. Which join do you use for "every customer and how many orders, even zero"?
Show answer
LEFT JOIN from customers to orders, with COUNT(orders.order_id) and a GROUP BY on the customer.