LEFT (OUTER) JOIN
The SQL LEFT JOIN
LEFT JOIN returns all records from the left table (table1), and the matching records from the right table (table2). When there is no match, the right-side columns are NULL.
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;Note: LEFT JOIN and LEFT OUTER JOIN are the same thing.
Example
Example
All customers and their orders, including customers who never ordered:
SELECT c.CustomerName, o.OrderID, o.Amount
FROM Customers c
LEFT JOIN Orders o ON o.CustomerID = c.CustomerID
ORDER BY c.CustomerName;Result: 10 rows
| CustomerName | OrderID | Amount |
|---|---|---|
| Aarav Traders | 104 | 56.25 |
| Aarav Traders | 101 | 245.5 |
| Blue Harbor Foods | 103 | 412 |
| Cedar & Co | 106 | 320.75 |
| Delta Mart | 107 | 27 |
| Delta Mart | 102 | 89.9 |
| Evergreen Grocers | 105 | 150 |
| Fjord Supplies | NULL | NULL |
| Golden Spoon | 108 | 198.4 |
| Harbor Lights | NULL | NULL |
Counting with LEFT JOIN
Example
Number of orders per customer. COUNT(o.OrderID) gives 0 for customers without orders, while COUNT(*) would wrongly give 1:
SELECT c.CustomerName, COUNT(o.OrderID) AS Orders, COALESCE(SUM(o.Amount), 0) AS Spent
FROM Customers c
LEFT JOIN Orders o ON o.CustomerID = c.CustomerID
GROUP BY c.CustomerName
ORDER BY Spent DESC;Result: 8 rows
| CustomerName | Orders | Spent |
|---|---|---|
| Blue Harbor Foods | 1 | 412 |
| Cedar & Co | 1 | 320.75 |
| Aarav Traders | 2 | 301.75 |
| Golden Spoon | 1 | 198.4 |
| Evergreen Grocers | 1 | 150 |
| Delta Mart | 2 | 116.9 |
| Harbor Lights | 0 | 0 |
| Fjord Supplies | 0 | 0 |
The Anti-Join Pattern
A very common question: "which rows in A have no match in B?" Do a LEFT JOIN and keep only rows where the right side is NULL.
Example
Customers who have never placed an order:
SELECT c.CustomerID, c.CustomerName
FROM Customers c
LEFT JOIN Orders o ON o.CustomerID = c.CustomerID
WHERE o.OrderID IS NULL;Result: 2 rows
| CustomerID | CustomerName |
|---|---|
| 6 | Fjord Supplies |
| 8 | Harbor Lights |
Products Never Sold
Example
Add a new product that nobody has ordered yet, then find products without any order line:
INSERT INTO Products (ProductID, ProductName, Category, Price, Stock)
VALUES (7, 'Saffron 1g', 'Grocery', 4.5, 15);
SELECT p.ProductName
FROM Products p
LEFT JOIN OrderDetails od ON od.ProductID = p.ProductID
WHERE od.OrderDetailID IS NULL;Result: 1 row
| ProductName |
|---|
| Saffron 1g |
Alternatives
-- NOT EXISTS (often the clearest, and safe with NULLs)
SELECT c.CustomerName FROM Customers c
WHERE NOT EXISTS (SELECT 1 FROM Orders o WHERE o.CustomerID = c.CustomerID);
-- NOT IN (careful: returns nothing if the subquery yields a NULL)
SELECT CustomerName FROM Customers
WHERE CustomerID NOT IN (SELECT CustomerID FROM Orders);Where the Condition Goes Matters
With an inner join, a condition gives the same result in ON or in WHERE. With an outer join it does not:
- In
ON: the condition decides which right-side rows match. Left rows are always kept. - In
WHERE: the condition filters the final rows, and can remove the left rows you wanted to keep.
Condition in ON
Example
All customers, with only their orders from March 2024:
SELECT c.CustomerName, o.OrderID, o.OrderDate
FROM Customers c
LEFT JOIN Orders o
ON o.CustomerID = c.CustomerID
AND o.OrderDate >= '2024-03-01'
ORDER BY c.CustomerName;Result: 8 rows
| CustomerName | OrderID | OrderDate |
|---|---|---|
| Aarav Traders | NULL | NULL |
| Blue Harbor Foods | NULL | NULL |
| Cedar & Co | 106 | 2024-03-09 |
| Delta Mart | 107 | 2024-03-22 |
| Evergreen Grocers | 105 | 2024-03-01 |
| Fjord Supplies | NULL | NULL |
| Golden Spoon | 108 | 2024-04-02 |
| Harbor Lights | NULL | NULL |
Condition in WHERE
Example
Moving the date test to WHERE turns it into an inner join: customers without March orders vanish.
SELECT c.CustomerName, o.OrderID, o.OrderDate
FROM Customers c
LEFT JOIN Orders o ON o.CustomerID = c.CustomerID
WHERE o.OrderDate >= '2024-03-01'
ORDER BY c.CustomerName;Result: 4 rows
| CustomerName | OrderID | OrderDate |
|---|---|---|
| Cedar & Co | 106 | 2024-03-09 |
| Delta Mart | 107 | 2024-03-22 |
| Evergreen Grocers | 105 | 2024-03-01 |
| Golden Spoon | 108 | 2024-04-02 |