SQL Left Join

A LEFT JOIN returns every row from the left table and fills in matching right-table columns, using NULL where no match is found.

What LEFT JOIN does

A LEFT JOIN (also written LEFT OUTER JOIN) guarantees that every row from the first, or left, table appears in the result at least once. When the database finds a matching row in the right table, it attaches that data as usual. When no match exists, it still keeps the left row but fills the right table's columns with NULL. This makes the LEFT JOIN ideal for questions like which customers have not placed any orders.

Every customer, with their orders if any

SELECT Customers.CustomerName, Orders.OrderID, Orders.Amount
FROM Customers
LEFT JOIN Orders
  ON Customers.CustomerID = Orders.CustomerID;

Here Customers is the left table, so all four customers appear. Anita Rao shows up twice because she has two orders. Chloe Dubois and Diego Alvarez have no orders, so their OrderID and Amount come back as NULL. Order 104 does not appear at all, because it belongs to the right table and has no matching customer.

CustomerNameOrderIDAmount
Anita Rao101250
Anita Rao10290
Ben Carter103400
Chloe DuboisNULLNULL
Diego AlvarezNULLNULL

Finding rows with no match

A common pattern is to LEFT JOIN and then keep only the rows where the right side came back NULL. This isolates the left-table records that have no counterpart, such as customers who have never ordered.

Customers who have never ordered

SELECT Customers.CustomerName
FROM Customers
LEFT JOIN Orders
  ON Customers.CustomerID = Orders.CustomerID
WHERE Orders.OrderID IS NULL;
Note: When you test for missing matches, check a column that can never legitimately be NULL, such as a primary key. Testing a nullable column could confuse a genuine NULL value with an unmatched row.
  • The table listed before LEFT JOIN is the left table, and all of its rows are preserved.
  • Unmatched right-table columns are returned as NULL.
  • Swapping the table order changes which rows are guaranteed to appear.
  • LEFT JOIN is the standard tool for reporting on records that may have no related data.