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_id | name | city | country | customer_since | segment | phone |
|---|---|---|---|---|---|---|
| 1 | Harbor Fremont Partners | Boston | USA | 2021-03-15 | Business | 617-555-0142 |
| 2 | Emily Carter | Chicago | USA | 2022-07-01 | Consumer | NULL |
| 3 | Riverside County Office | Riverside | USA | 2020-11-20 | Government | 951-555-0110 |
| 4 | Raj Patel | Leeds | UK | 2023-01-09 | Consumer | 0113 496 0123 |
| 5 | Lakeview Hotel Ltd | Manchester | UK | 2022-04-18 | Business | 0161 496 0456 |
| 6 | Olivia Brooks | Austin | USA | 2024-02-27 | Consumer | NULL |
| 7 | Greene & Shaw LLP | London | UK | 2021-09-05 | Business | 020 7946 0321 |
| 8 | Maple Grove High School | Austin | USA | 2023-08-14 | School | 512-555-0187 |
| 9 | Chloe Evans | Boston | USA | 2025-05-30 | Consumer | 617-555-0199 |
| 10 | Northline Printing Co | Toronto | Canada | 2020-06-12 | Business | 416-555-0133 |
| 11 | Daniel Kim | Seattle | USA | 2024-10-03 | Consumer | NULL |
| 12 | Westbrook Dental Clinic | Chicago | USA | 2022-12-01 | Business | 312-555-0164 |
| 15 | Harlow Street Bakery | Glasgow | UK | 2026-06-08 | Business | 0141 496 0574 |
| 16 | Jordan Ellis | Portland | USA | 2026-06-11 | Consumer | NULL |
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
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_name | last_name | order_id | status |
|---|---|---|---|
| David | Ross | 1005 | cancelled |
| David | Ross | 1010 | delivered |
| David | Ross | 1013 | cancelled |
| Laura | Hughes | NULL | NULL |
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_name | last_name | department |
|---|---|---|
| Karen | Mitchell | Management |
| Sarah | Long | Warehouse |
| Felix | Turner | Warehouse |
| Nina | Cooper | Accounting |
| Laura | Hughes | Customer service |
| Paul | Ward | Accounting |
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_id | name | city | country | customer_since | segment | phone |
|---|---|---|---|---|---|---|
| 1 | Harbor Fremont Partners | Boston | USA | 2021-03-15 | Business | 617-555-0142 |
| 2 | Emily Carter | Chicago | USA | 2022-07-01 | Consumer | NULL |
| 3 | Riverside County Office | Riverside | USA | 2020-11-20 | Government | 951-555-0110 |
| 4 | Raj Patel | Leeds | UK | 2023-01-09 | Consumer | 0113 496 0123 |
| 5 | Lakeview Hotel Ltd | Manchester | UK | 2022-04-18 | Business | 0161 496 0456 |
| 6 | Olivia Brooks | Austin | USA | 2024-02-27 | Consumer | NULL |
| 7 | Greene & Shaw LLP | London | UK | 2021-09-05 | Business | 020 7946 0321 |
| 8 | Maple Grove High School | Austin | USA | 2023-08-14 | School | 512-555-0187 |
| 9 | Chloe Evans | Boston | USA | 2025-05-30 | Consumer | 617-555-0199 |
| 10 | Northline Printing Co | Toronto | Canada | 2020-06-12 | Business | 416-555-0133 |
| 11 | Daniel Kim | Seattle | USA | 2024-10-03 | Consumer | NULL |
| 12 | Westbrook Dental Clinic | Chicago | USA | 2022-12-01 | Business | 312-555-0164 |
| 15 | Harlow Street Bakery | Glasgow | UK | 2026-06-08 | Business | 0141 496 0574 |
| 16 | Jordan Ellis | Portland | USA | 2026-06-11 | Consumer | NULL |
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
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_id | name | city | country | customer_since | segment | phone |
|---|---|---|---|---|---|---|
| 1 | Harbor Fremont Partners | Boston | USA | 2021-03-15 | Business | 617-555-0142 |
| 2 | Emily Carter | Chicago | USA | 2022-07-01 | Consumer | NULL |
| 3 | Riverside County Office | Riverside | USA | 2020-11-20 | Government | 951-555-0110 |
| 4 | Raj Patel | Leeds | UK | 2023-01-09 | Consumer | 0113 496 0123 |
| 5 | Lakeview Hotel Ltd | Manchester | UK | 2022-04-18 | Business | 0161 496 0456 |
| 6 | Olivia Brooks | Austin | USA | 2024-02-27 | Consumer | NULL |
| 7 | Greene & Shaw LLP | London | UK | 2021-09-05 | Business | 020 7946 0321 |
| 8 | Maple Grove High School | Austin | USA | 2023-08-14 | School | 512-555-0187 |
| 9 | Chloe Evans | Boston | USA | 2025-05-30 | Consumer | 617-555-0199 |
| 10 | Northline Printing Co | Toronto | Canada | 2020-06-12 | Business | 416-555-0133 |
| 11 | Daniel Kim | Seattle | USA | 2024-10-03 | Consumer | NULL |
| 12 | Westbrook Dental Clinic | Chicago | USA | 2022-12-01 | Business | 312-555-0164 |
| 15 | Harlow Street Bakery | Glasgow | UK | 2026-06-08 | Business | 0141 496 0574 |
| 16 | Jordan Ellis | Portland | USA | 2026-06-11 | Consumer | NULL |
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
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.