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 SyntaxSELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name;
Note: In some databases,
LEFT JOINis calledLEFT OUTER JOIN. They are the same.
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 ExampleThe following SQL statement will select all customers, and any orders they might have.
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.