SQL LEFT JOIN
LEFT JOIN returns every row from the left table, plus matching rows from the right table. Where there is no match, the right-hand columns are NULL.
A classic use: find things that have no match, such as customers who never ordered. LEFT JOIN, then keep rows where the right side IS NULL.
Syntax
SELECT a.col, b.col FROM table_a a LEFT JOIN table_b b ON b.a_id = a.id;
Try it yourself
Run the example below on a real PostgreSQL database, then change it and run it again. Nothing to install, no sign-up.
Examples
1. Keep everyone
SELECT e.first_name, d.name FROM employees e LEFT JOIN departments d ON d.id = e.department_id;
Employees without a department show NULL.
2. Find missing matches
SELECT c.id, c.first_name FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id IS NULL;
Customers who never ordered.
Common mistakes
- Filtering the right table in WHERE (e.g. WHERE o.status = 'delivered'), which silently turns the LEFT JOIN into an INNER JOIN. Put that condition in ON.
Practice: test yourself
This challenge is from our interactive course. Write the query and press Check answer.
Return id, first_name and last_name of customers who have never placed an order.
FAQ
LEFT JOIN or LEFT OUTER JOIN?
They are the same. OUTER is optional.
When should I use RIGHT JOIN?
Almost never. Swap the table order and use LEFT JOIN; it is easier to read.
Learn SQL step by step
65 hands-on exercises, streaks, a leaderboard and a certificate.