Introduction
SQL Server LEFT JOIN clause is used to retrieve all records from the left table and matching records from the right table. If a record in the left table does not have a matching record in the right table, SQL Server returns NULL values for the columns from the right table.
A LEFT JOIN is also known as a LEFT OUTER JOIN. Both terms represent the same operation in SQL Server.In relational databases, data is often stored in multiple related tables.
This JOIN type is particularly useful when you need to preserve all records from one table while retrieving related information from another table.
SQL Server LEFT JOIN Syntax
The basic syntax of a LEFT JOIN in SQL Server is:
SELECT
columns
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;
You can also write LEFT OUTER JOIN instead of LEFT JOIN:
SELECT
columns
FROM table1
LEFT OUTER JOIN table2
ON table1.common_column = table2.common_column;
Both statements produce the same result.
Syntax explanation:
SELECT: Specifies the columns to retrieve.FROM: Defines the left table.LEFT JOIN: Specifies the right table to join.ON: Defines the matching condition.table1.common_column: The column from the left table.table2.common_column: The related column from the right table.
The left table is the table written immediately after FROM, while the right table is the table written after LEFT JOIN.
SQL Server LEFT JOIN Example
Let’s understand how LEFT JOIN works using two sample tables named Employees and Departments.
Employees Table
| EmployeeID | EmployeeName | DepartmentID |
|---|---|---|
| 1 | John | 10 |
| 2 | David | 20 |
| 3 | Sarah | 10 |
| 4 | Michael | 30 |
| 5 | Robert | 40 |
Departments Table
| DepartmentID | DepartmentName |
|---|---|
| 10 | IT |
| 20 | HR |
| 30 | Finance |
| 50 | Marketing |
Suppose you want to retrieve all employees and their corresponding department names.
SELECT
e.EmployeeID,
e.EmployeeName,
e.DepartmentID,
d.DepartmentName
FROM Employees AS e
LEFT JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID;
Result
| EmployeeID | EmployeeName | DepartmentID | DepartmentName |
|---|---|---|---|
| 1 | John | 10 | IT |
| 2 | David | 20 | HR |
| 3 | Sarah | 10 | IT |
| 4 | Michael | 30 | Finance |
| 5 | Robert | 40 | NULL |
The query returns all five employees from the Employees table.
Robert has DepartmentID 40, but there is no matching department in the Departments table. Therefore, SQL Server returns NULL for DepartmentName.
The Marketing department is not included because LEFT JOIN preserves all rows from the left table, not unmatched rows from the right table.
SQL Server LEFT OUTER JOIN
LEFT OUTER JOIN is another way to write LEFT JOIN in SQL Server. The OUTER keyword is optional.
For example:
SELECT
e.EmployeeID,
e.EmployeeName,
d.DepartmentName
FROM Employees AS e
LEFT OUTER JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID;
This query returns the same result as the following statement:
SELECT
e.EmployeeID,
e.EmployeeName,
d.DepartmentName
FROM Employees AS e
LEFT JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID;
You can use either form depending on your preferred SQL coding style.
SQL Server LEFT JOIN with Table Alias
Table aliases make LEFT JOIN queries shorter and easier to understand. They are especially useful when joining multiple tables.
For example:
SELECT
e.EmployeeID,
e.EmployeeName,
d.DepartmentName
FROM Employees AS e
LEFT JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID;
Here:
eis the alias for the Employees table.dis the alias for the Departments table.e.DepartmentIDrefers to the DepartmentID column in Employees.d.DepartmentNamerefers to the DepartmentName column in Departments.
Aliases do not change the original table names. They are temporary names used within the query.
SQL Server LEFT JOIN with WHERE Clause
You can combine LEFT JOIN with a WHERE clause to filter the returned records.
For example, retrieve all employees who belong to the IT department:
SELECT
e.EmployeeID,
e.EmployeeName,
d.DepartmentName
FROM Employees AS e
LEFT JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID
WHERE d.DepartmentName = 'IT';
This query returns employees whose matching department name is IT.
However, the WHERE condition filters out rows where DepartmentName is NULL. Therefore, the result no longer includes employees without a matching department.
If you want to preserve all employees while restricting which department records can match, place the condition in the ON clause instead:
SELECT
e.EmployeeID,
e.EmployeeName,
d.DepartmentName
FROM Employees AS e
LEFT JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID
AND d.DepartmentName = 'IT';
This query returns every employee. The DepartmentName column contains IT for matching employees and NULL for employees without a matching IT department.
Understanding the difference between conditions in ON and WHERE is important when working with outer joins.
SQL Server LEFT JOIN with IS NULL
One common use of LEFT JOIN is identifying records in the left table that do not have matching records in the right table.
You can use the IS NULL condition to find these unmatched records.
For example, retrieve employees who do not have a matching department:
SELECT
e.EmployeeID,
e.EmployeeName,
e.DepartmentID
FROM Employees AS e
LEFT JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID
WHERE d.DepartmentID IS NULL;
Result
| EmployeeID | EmployeeName | DepartmentID |
|---|---|---|
| 5 | Robert | 40 |
This query returns employees whose DepartmentID does not match any DepartmentID in the Departments table.
The right-side column used in the IS NULL condition should be a column that cannot be NULL in a valid matching record, such as a primary key.
This technique is useful for finding unmatched records between related tables.
SQL Server LEFT JOIN with Multiple Tables
SQL Server allows you to use multiple LEFT JOIN clauses in a single query. This is useful when you need information from several related tables while preserving all records from the original left-side table.
Suppose an application has three tables:
Orders: Contains order information.Customers: Contains customer details.Payments: Contains payment information.
You can retrieve all orders with their customer and payment details:
SELECT
o.OrderID,
c.CustomerName,
p.PaymentDate,
p.PaymentAmount
FROM Orders AS o
LEFT JOIN Customers AS c
ON o.CustomerID = c.CustomerID
LEFT JOIN Payments AS p
ON o.OrderID = p.OrderID;
The query preserves all records from Orders. If an order does not have a matching customer or payment record, the corresponding columns contain NULL.
Multiple LEFT JOIN clauses are useful for generating reports where the primary records must remain visible even when related information is unavailable.
SQL Server LEFT JOIN with GROUP BY
You can combine LEFT JOIN with GROUP BY and aggregate functions to generate summary reports.
For example, calculate the total number of employees in each department:
SELECT
d.DepartmentID,
d.DepartmentName,
COUNT(e.EmployeeID) AS TotalEmployees
FROM Departments AS d
LEFT JOIN Employees AS e
ON d.DepartmentID = e.DepartmentID
GROUP BY
d.DepartmentID,
d.DepartmentName;
This query starts with Departments as the left table, so all departments are included, even when no employees belong to them.
Using COUNT(e.EmployeeID) is important because COUNT(column) ignores NULL values. A department without matching employees therefore receives a count of zero.
If you use COUNT(*) instead, the unmatched department row itself is counted, producing a count of one.
SQL Server LEFT JOIN with ORDER BY
You can use ORDER BY with LEFT JOIN to sort the resulting records.
For example, retrieve all employees and sort them by department name:
SELECT
e.EmployeeID,
e.EmployeeName,
d.DepartmentName
FROM Employees AS e
LEFT JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID
ORDER BY d.DepartmentName ASC;
You can also sort by employee name:
SELECT
e.EmployeeID,
e.EmployeeName,
d.DepartmentName
FROM Employees AS e
LEFT JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID
ORDER BY e.EmployeeName DESC;
The ORDER BY clause sorts the final result set without changing the matching behavior of the LEFT JOIN.
SQL Server LEFT JOIN with Aggregate Functions
Aggregate functions such as COUNT(), SUM(), AVG(), MIN(), and MAX() can be used with LEFT JOIN to calculate summary information.
For example, suppose you want to retrieve all customers and the total amount of their orders.
SELECT
c.CustomerID,
c.CustomerName,
SUM(o.TotalAmount) AS TotalOrderAmount
FROM Customers AS c
LEFT JOIN Orders AS o
ON c.CustomerID = o.CustomerID
GROUP BY
c.CustomerID,
c.CustomerName;
Customers without orders are also included. Their TotalOrderAmount will be NULL because SUM() returns NULL when there are no non-NULL input values.
You can use COALESCE() to display zero instead:
SELECT
c.CustomerID,
c.CustomerName,
COALESCE(SUM(o.TotalAmount), 0) AS TotalOrderAmount
FROM Customers AS c
LEFT JOIN Orders AS o
ON c.CustomerID = o.CustomerID
GROUP BY
c.CustomerID,
c.CustomerName;
Now customers without orders will have a total order amount of zero.
SQL Server LEFT JOIN with Subquery
You can use a subquery with LEFT JOIN to combine a table with a calculated or filtered result set.
For example, retrieve all customers along with their order counts:
SELECT
c.CustomerID,
c.CustomerName,
COALESCE(o.TotalOrders, 0) AS TotalOrders
FROM Customers AS c
LEFT JOIN
(
SELECT
CustomerID,
COUNT(*) AS TotalOrders
FROM Orders
GROUP BY CustomerID
) AS o
ON c.CustomerID = o.CustomerID;
The subquery calculates the number of orders for each customer. The LEFT JOIN then combines that summary with all customer records.
Customers without orders receive zero through the COALESCE() function.
SQL Server LEFT JOIN with UPDATE
You can use LEFT JOIN in an UPDATE statement to modify records based on related table information.
For example, update the department name in an employee-related reporting table using matching department records:
UPDATE e
SET e.DepartmentName = d.DepartmentName
FROM EmployeeReports AS e
LEFT JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID;
This statement updates EmployeeReports using matching department names. When no department matches, the assigned DepartmentName becomes NULL.
Before executing an UPDATE statement, it is useful to run an equivalent SELECT query to inspect the records and values that will be affected.