TOP

SQL LEFT JOIN

SQL LEFT JOIN Description

The keyword LEFT JOIN returns all records from the left table (table1) and the corresponding records from the right table (table2).

The result will be 0 entries on the right side if there is no match.


LEFT JOIN Syntax

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name
In some databases, LEFT JOIN is called LEFT OUTER JOIN.

SQL INNER JOIN

Demonstration database

In this tutorial we will use the famous example database "Northwind".

Below is a sample from the table "Customers" ("Customers"):

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.5021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.5023Mexico
4Around the HornThomas Hardy120 Hanover Sq.LondonWA1 1DPUK
5Berglunds snabbköpChristina BerglundBerguvsvägen 8LuleåS-958 22Sweden

And the sample from the table "Orders" ("Orders"):

ProductIDOrderIDCustomerIDEmployeeIDOrderDateShipperID
1102489051996-07-043
2102498161996-07-051
3102503441996-07-082
4102518431996-07-081
5102527641996-07-092

SQL LEFT JOIN Example

The following SQL statement will select all customers and any orders they may have:

Run SQLSELECT Customers.CustomerName, Orders.OrderID 
FROM Customers 
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID 
ORDER BY Customers.CustomerName
The LEFT JOIN keyword returns all records from the left table (Customers), even if there are no matches in the right table (Orders).