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

CustomerNameOrderIDAmount
Aarav Traders10456.25
Aarav Traders101245.5
Blue Harbor Foods103412
Cedar & Co106320.75
Delta Mart10727
Delta Mart10289.9
Evergreen Grocers105150
Fjord SuppliesNULLNULL
Golden Spoon108198.4
Harbor LightsNULLNULL

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

CustomerNameOrdersSpent
Blue Harbor Foods1412
Cedar & Co1320.75
Aarav Traders2301.75
Golden Spoon1198.4
Evergreen Grocers1150
Delta Mart2116.9
Harbor Lights00
Fjord Supplies00

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

CustomerIDCustomerName
6Fjord Supplies
8Harbor 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

CustomerNameOrderIDOrderDate
Aarav TradersNULLNULL
Blue Harbor FoodsNULLNULL
Cedar & Co1062024-03-09
Delta Mart1072024-03-22
Evergreen Grocers1052024-03-01
Fjord SuppliesNULLNULL
Golden Spoon1082024-04-02
Harbor LightsNULLNULL

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

CustomerNameOrderIDOrderDate
Cedar & Co1062024-03-09
Delta Mart1072024-03-22
Evergreen Grocers1052024-03-01
Golden Spoon1082024-04-02