SQL LEFT JOIN: keeping every row, explained and practiced

LEFT JOIN connects two tables just like INNER JOIN, but never drops a row from the left table. Customers who haven't ordered, employees with no assignments, products nobody has bought: LEFT JOIN keeps them on the list, and lets you go looking for them on purpose.

In short: SELECT c.name, o.order_id FROM customers AS c LEFT JOIN orders AS o ON o.customer_id = c.customer_id; returns every customer. A customer with no orders gets one row with NULL in the columns from orders. Add WHERE o.order_id IS NULL and only those customers are left.

What you should know first: joining two tables with INNER JOIN and finding missing values with IS NULL.

Try it now

In the exercises on this page, two new customers have signed up: Harlow Street Bakery and Jordan Ellis. Neither has ordered anything yet, so an INNER JOIN would swallow them.

Task Two new customers have signed up but haven't ordered anything yet. Management still wants every customer on the list: name from customers, plus order_id and order_date of each of their orders. Customers with no orders should appear with empty fields.

customers 14 rows, 7 columns
customer_idnamecitycountrycustomer_sincesegmentphone
1Harbor Fremont PartnersBostonUSA2021-03-15Business617-555-0142
2Emily CarterChicagoUSA2022-07-01ConsumerNULL
3Riverside County OfficeRiversideUSA2020-11-20Government951-555-0110
4Raj PatelLeedsUK2023-01-09Consumer0113 496 0123
5Lakeview Hotel LtdManchesterUK2022-04-18Business0161 496 0456
6Olivia BrooksAustinUSA2024-02-27ConsumerNULL
7Greene & Shaw LLPLondonUK2021-09-05Business020 7946 0321
8Maple Grove High SchoolAustinUSA2023-08-14School512-555-0187
9Chloe EvansBostonUSA2025-05-30Consumer617-555-0199
10Northline Printing CoTorontoCanada2020-06-12Business416-555-0133
11Daniel KimSeattleUSA2024-10-03ConsumerNULL
12Westbrook Dental ClinicChicagoUSA2022-12-01Business312-555-0164
15Harlow Street BakeryGlasgowUK2026-06-08Business0141 496 0574
16Jordan EllisPortlandUSA2026-06-11ConsumerNULL
orders 18 rows, 5 columns
order_idcustomer_idorder_datestatusemployee_id
100112026-01-08delivered3
100242026-01-12delivered4
100332026-01-20delivered3
100472026-02-03delivered4
100522026-02-11cancelled8
100612026-02-25delivered3
1007102026-03-04delivered2
100852026-03-17delivered4
100982026-03-30delivered3
101062026-04-07delivered8
1011122026-04-22delivered4
101232026-05-06delivered3
101392026-05-19cancelled8
101472026-06-02shipped4
101512026-06-10shipped3
1016112026-06-12openNULL
101742026-06-13openNULL
1018102026-06-14openNULL

How LEFT JOIN works

The database walks through the left table, the one after FROM, row by row. For each row it looks for partners in the right table, just as with INNER JOIN. Every partner it finds produces one row. If it finds none, it keeps the row anyway and fills every column from the right table with NULL. Here's an example with employees, since each order records who handled it:

SELECT e.first_name, e.last_name, o.order_id, o.status
FROM employees AS e
LEFT JOIN orders AS o ON o.employee_id = e.employee_id
WHERE e.department = 'Customer service'
ORDER BY e.employee_id, o.order_id;
first_namelast_nameorder_idstatus
DavidRoss1005cancelled
DavidRoss1010delivered
DavidRoss1013cancelled
LauraHughesNULLNULL

David Ross has handled three orders, so he appears three times. Laura Hughes is new and hasn't handled any; she stays in with NULLs. An INNER JOIN would leave her out. The department filter can sit in WHERE here because it's about the left table.

Table order matters. The table to the left of LEFT JOIN is the one kept in full. FROM orders LEFT JOIN employees would keep every order, including the three open ones with nobody assigned, but no employee without orders.

Finding rows with no match

Often the NULL rows are exactly what you're after: customers with no orders, products that never sold, staff with nothing assigned. After the LEFT JOIN, filter on a column from the right table that is never empty for a real match, ideally its key:

SELECT e.first_name, e.last_name, e.department
FROM employees AS e
LEFT JOIN orders AS o ON o.employee_id = e.employee_id
WHERE o.order_id IS NULL;
first_namelast_namedepartment
KarenMitchellManagement
SarahLongWarehouse
FelixTurnerWarehouse
NinaCooperAccounting
LauraHughesCustomer service
PaulWardAccounting

This pattern is often called an anti-join: every row on the left that has nothing matching on the right.

Practice: customers with no orders

Now the same idea with customers. Who has never ordered?

Task Marketing wants to send a welcome voucher to customers who have never placed an order. Show the name and city of every customer without a single order.

customers 14 rows, 7 columns
customer_idnamecitycountrycustomer_sincesegmentphone
1Harbor Fremont PartnersBostonUSA2021-03-15Business617-555-0142
2Emily CarterChicagoUSA2022-07-01ConsumerNULL
3Riverside County OfficeRiversideUSA2020-11-20Government951-555-0110
4Raj PatelLeedsUK2023-01-09Consumer0113 496 0123
5Lakeview Hotel LtdManchesterUK2022-04-18Business0161 496 0456
6Olivia BrooksAustinUSA2024-02-27ConsumerNULL
7Greene & Shaw LLPLondonUK2021-09-05Business020 7946 0321
8Maple Grove High SchoolAustinUSA2023-08-14School512-555-0187
9Chloe EvansBostonUSA2025-05-30Consumer617-555-0199
10Northline Printing CoTorontoCanada2020-06-12Business416-555-0133
11Daniel KimSeattleUSA2024-10-03ConsumerNULL
12Westbrook Dental ClinicChicagoUSA2022-12-01Business312-555-0164
15Harlow Street BakeryGlasgowUK2026-06-08Business0141 496 0574
16Jordan EllisPortlandUSA2026-06-11ConsumerNULL
orders 18 rows, 5 columns
order_idcustomer_idorder_datestatusemployee_id
100112026-01-08delivered3
100242026-01-12delivered4
100332026-01-20delivered3
100472026-02-03delivered4
100522026-02-11cancelled8
100612026-02-25delivered3
1007102026-03-04delivered2
100852026-03-17delivered4
100982026-03-30delivered3
101062026-04-07delivered8
1011122026-04-22delivered4
101232026-05-06delivered3
101392026-05-19cancelled8
101472026-06-02shipped4
101512026-06-10shipped3
1016112026-06-12openNULL
101742026-06-13openNULL
1018102026-06-14openNULL

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

Condition in ON or in WHERE?

This is the classic LEFT JOIN mistake. Say you want every employee, with only their shipped orders next to them. Put the status condition in ON and it applies while partners are being matched:

SELECT e.first_name, e.last_name, o.order_id
FROM employees AS e
LEFT JOIN orders AS o
  ON o.employee_id = e.employee_id AND o.status = 'shipped';

Result: all 10 employees. Julia Reyes has 1015, Marcus Allen has 1014, everyone else has NULL. Move the same condition into WHERE and the database checks it after the join. For the NULL rows, o.status = 'shipped' isn't true, so they're thrown out and 2 rows remain, exactly as if you had written an INNER JOIN.

Rule of thumb: with LEFT JOIN, conditions on the right table belong in ON. Conditions on the left table, and the IS NULL check for missing matches, belong in WHERE.

With INNER JOIN it makes no difference; both versions return the same rows.

One more, aimed at exactly that

This exercise notices whether your condition is in the right place.

Task Customer service is calling every customer this week. The call sheet lists each customer's name, with the order_id of their open order (status open) next to it. Customers without an open order stay on the sheet with an empty order_id.

customers 14 rows, 7 columns
customer_idnamecitycountrycustomer_sincesegmentphone
1Harbor Fremont PartnersBostonUSA2021-03-15Business617-555-0142
2Emily CarterChicagoUSA2022-07-01ConsumerNULL
3Riverside County OfficeRiversideUSA2020-11-20Government951-555-0110
4Raj PatelLeedsUK2023-01-09Consumer0113 496 0123
5Lakeview Hotel LtdManchesterUK2022-04-18Business0161 496 0456
6Olivia BrooksAustinUSA2024-02-27ConsumerNULL
7Greene & Shaw LLPLondonUK2021-09-05Business020 7946 0321
8Maple Grove High SchoolAustinUSA2023-08-14School512-555-0187
9Chloe EvansBostonUSA2025-05-30Consumer617-555-0199
10Northline Printing CoTorontoCanada2020-06-12Business416-555-0133
11Daniel KimSeattleUSA2024-10-03ConsumerNULL
12Westbrook Dental ClinicChicagoUSA2022-12-01Business312-555-0164
15Harlow Street BakeryGlasgowUK2026-06-08Business0141 496 0574
16Jordan EllisPortlandUSA2026-06-11ConsumerNULL
orders 18 rows, 5 columns
order_idcustomer_idorder_datestatusemployee_id
100112026-01-08delivered3
100242026-01-12delivered4
100332026-01-20delivered3
100472026-02-03delivered4
100522026-02-11cancelled8
100612026-02-25delivered3
1007102026-03-04delivered2
100852026-03-17delivered4
100982026-03-30delivered3
101062026-04-07delivered8
1011122026-04-22delivered4
101232026-05-06delivered3
101392026-05-19cancelled8
101472026-06-02shipped4
101512026-06-10shipped3
1016112026-06-12openNULL
101742026-06-13openNULL
1018102026-06-14openNULL

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

Common mistakes

INNER JOIN instead of LEFT JOIN

With INNER JOIN, customers without orders vanish. The first exercise reports "Your query returns only 18 rows. Wanted: 20 rows." When you're hunting for customers with no orders, INNER JOIN returns nothing at all.

Tables the wrong way round

FROM orders LEFT JOIN customers keeps every order, not every customer, and the first exercise again reports too few rows. The table you want complete goes right after FROM.

= NULL instead of IS NULL

WHERE o.order_id = NULL is never true, and the exercise tells you: "With = NULL, SQL never finds anything." Missing values are found only with IS NULL.

Checking the wrong column for NULL

Test a column that's always filled for a real match. WHERE o.employee_id IS NULL also catches customers whose open orders haven't been assigned yet: "Your query returns 5 rows. Wanted: 2 rows."

Right-table condition in WHERE

The mistake from the section above. In the third exercise it shows up as "Your query returns only 3 rows. Wanted: 14 rows." Move the condition into ON with AND.

LEFT JOIN in MySQL, PostgreSQL, SQL Server and SQLite

LEFT JOIN behaves the same in all four, and so does the ON vs WHERE difference. The PostgreSQL docs and Microsoft Learn explain it in almost the same words: a condition in ON is applied before the join, one in WHERE after it, and that only changes the result for outer joins. LEFT OUTER JOIN is simply the long form of LEFT JOIN everywhere. For RIGHT JOIN, the MySQL manual recommends using LEFT JOIN instead so your code stays portable. SQLite only supports RIGHT JOIN from version 3.39.0.

FAQ

What's the difference between LEFT JOIN and LEFT OUTER JOIN?

None. OUTER is an optional word; both mean the same thing.

What's the difference between LEFT JOIN and INNER JOIN?

INNER JOIN returns only rows that have a partner. LEFT JOIN returns those same rows plus every left row without a partner, with NULLs on the right. If every left row has a partner, the results are identical.

Why does my result have more rows than the left table?

LEFT JOIN keeps every left row at least once, but a row with three partners appears three times. More rows than the left table is normal.

How do I count orders per customer, including zeros?

Use LEFT JOIN with GROUP BY, and count COUNT(o.order_id) rather than COUNT(*). COUNT(*) also counts the NULL row and reports 1 for customers with no orders. COUNT(column) only counts actual values and correctly reports 0.

Can I show text instead of NULL?

Yes, with COALESCE, for example COALESCE(o.status, 'no orders'). You met it in the lesson on NULL in SQL.

Up next: joining multiple tables

Customer, order, order line, product: real reports chain three or four tables together. You'll calculate revenue per customer and learn to spot rows that get counted twice.

Practice joining multiple tables

Write to us

Choose language

Select your preferred language