SQL LEFT JOIN

The SQL LEFT JOIN Keyword

The LEFT JOIN keyword returns all records from the left table (table1), and the matching records from the right table (table2). The result is NULL from the right side if there is no match.

!LEFT JOIN Diagram


LEFT JOIN Syntax

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

Note: In some databases, LEFT JOIN is called LEFT OUTER JOIN. They are the same.


Demo Database

Customers Table:

CustomerID CustomerName
1 Alfreds Futterkiste
2 Ana Trujillo Emparedados
3 Antonio Moreno Taquería
4 Around the Horn

Orders Table:

OrderID CustomerID
10308 2
10309 3
10310 1

LEFT JOIN Example

The following SQL statement will select all customers, and any orders they might have.

LEFT JOIN Example

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

The result set would look like this:

CustomerName OrderID
Alfreds Futterkiste 10310
Ana Trujillo Emparedados 10308
Antonio Moreno Taquería 10309
Around the Horn NULL

Notice that "Around the Horn", who has no orders, is still included in the result set. The OrderID for this customer is NULL.