SQL Left Join
SQL LEFT JOIN
LEFT JOIN returns all rows from the left table and the matching rows from the right table. If there is no match, NULL values are returned for the right table columns.
Basic Syntax
SELECT columns FROM table1 LEFT JOIN table2 ON table1.column = table2.column;
Example: All Customers and Their Orders
SELECT c.name, o.order_date FROM customers AS c LEFT JOIN orders AS o ON c.id = o.customer_id;
This query returns all customers. If a customer has no orders, order_date will be NULL.
INNER JOIN vs LEFT JOIN
- INNER JOIN returns only matching rows
- LEFT JOIN returns all rows from the left table, even if there is no match
Understanding NULL Results
If there is no matching row in the right table, all selected columns from the right table will be NULL.
LEFT JOIN with WHERE (Important)
Be careful when adding conditions on the right table in the WHERE clause. It may turn your LEFT JOIN into an INNER JOIN.
Incorrect example:
SELECT c.name, o.order_date FROM customers AS c LEFT JOIN orders AS o ON c.id = o.customer_id WHERE o.order_date > '2026-01-01';
This filters out NULL rows and behaves like INNER JOIN.
Correct Filtering in LEFT JOIN
If you want to preserve unmatched rows, move conditions into the ON clause:
SELECT c.name, o.order_date FROM customers AS c LEFT JOIN orders AS o ON c.id = o.customer_id AND o.order_date > '2026-01-01';
Finding Unmatched Rows (Anti-Join)
You can use LEFT JOIN with IS NULL to find rows without matches.
SELECT c.name FROM customers AS c LEFT JOIN orders AS o ON c.id = o.customer_id WHERE o.id IS NULL;
This returns customers who have never placed an order.
Performance Note
Indexes on join columns improve LEFT JOIN performance, especially on large tables.
Next Step
Continue with SQL RIGHT JOIN to understand the reverse behavior.