Introduction
SQL Server full outer join Clause is used to retrieve all records from both tables, including matching and non-matching records. When a record in one table does not have a corresponding record in the other table, SQL Server returns NULL values for the columns of the table without a match.
FULL OUTER JOIN is also known as FULL JOIN. Both terms work in the same way in SQL Server.
SQL Server FULL OUTER JOIN Syntax
The following is the basic syntax of the FULL OUTER JOIN clause in SQL Server:
SELECT
column_name1,
column_name2
FROM
left_table
FULL OUTER JOIN
right_table
ON
left_table.common_column = right_table.common_column;
Syntax explanation:
SELECT: Specifies the columns to retrieve.FROM: Defines the first or left table.FULL OUTER JOIN: Returns matching and non-matching records from both tables.ON: Specifies the condition used to match records.left_table: The table written before FULL OUTER JOIN.right_table: The table written after FULL OUTER JOIN.
The most important feature of FULL OUTER JOIN is that it preserves all rows from both tables, whether or not matching records exist.
SQL Server FULL OUTER JOIN Example
Let’s understand how FULL OUTER JOIN works with a practical example.
Suppose we have two tables: Employees and Departments.
Employees Table
| EmployeeID | EmployeeName | DepartmentID |
|---|---|---|
| 1 | John | 101 |
| 2 | David | 102 |
| 3 | Sarah | 101 |
| 4 | Michael | 106 |
Departments Table
| DepartmentID | DepartmentName |
|---|---|
| 101 | IT |
| 102 | HR |
| 103 | Finance |
| 104 | Marketing |
Notice that the Employees table contains an employee assigned to DepartmentID 106, which does not exist in the Departments table. Similarly, the Departments table contains departments 103 and 104 that have no matching employees.
Now, let’s retrieve all records from both tables using FULL OUTER JOIN.
SELECT
e.EmployeeID,
e.EmployeeName,
d.DepartmentID,
d.DepartmentName
FROM Employees AS e
FULL OUTER JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID;
Output:
| EmployeeID | EmployeeName | DepartmentID | DepartmentName |
|---|---|---|---|
| 1 | John | 101 | IT |
| 3 | Sarah | 101 | IT |
| 2 | David | 102 | HR |
| NULL | NULL | 103 | Finance |
| NULL | NULL | 104 | Marketing |
| 4 | Michael | 106 | NULL |
Explanation:
- John and Sarah are matched with the IT department.
- David is matched with the HR department.
- Finance and Marketing appear even though they have no matching employees.
- Michael appears even though DepartmentID 106 does not exist in the Departments table.
- SQL Server returns NULL for columns where no matching record exists.
This example demonstrates how FULL OUTER JOIN returns every record from both tables.
SQL Server FULL JOIN
SQL Server supports both FULL JOIN and FULL OUTER JOIN. They are equivalent and return the same results.
SELECT
e.EmployeeName,
d.DepartmentName
FROM Employees AS e
FULL JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID;
The OUTER keyword is optional. You can use either syntax depending on your coding preference.
SQL Server FULL OUTER JOIN with Table Aliases
Table aliases make queries shorter and easier to understand. They are particularly useful when joining multiple tables.
SELECT
e.EmployeeID,
e.EmployeeName,
d.DepartmentName
FROM Employees AS e
FULL OUTER JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID;
Explanation:
erepresents the Employees table.drepresents the Departments table.- The ON condition matches employees and departments using DepartmentID.
Using aliases helps improve query readability and reduces repetitive table names.
SQL Server FULL OUTER JOIN with WHERE Clause
You can use the WHERE clause with FULL OUTER JOIN to filter the combined result.
For example, the following query retrieves records where either the employee name is John or the department name is Finance.
SELECT
e.EmployeeName,
d.DepartmentName
FROM Employees AS e
FULL OUTER JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID
WHERE e.EmployeeName = 'John'
OR d.DepartmentName = 'Finance';
Explanation:
The FULL OUTER JOIN first produces the combined result. The WHERE clause then filters that result based on the specified conditions.
When filtering outer join results, remember that a WHERE condition can remove rows containing NULL values. Therefore, choose filtering conditions carefully.
SQL Server FULL OUTER JOIN with IS NULL
You can use FULL OUTER JOIN with IS NULL to identify unmatched records from both tables.
For example, the following query finds employees without matching departments and departments without matching employees.
SELECT
e.EmployeeID,
e.EmployeeName,
d.DepartmentID,
d.DepartmentName
FROM Employees AS e
FULL OUTER JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID
WHERE e.EmployeeID IS NULL
OR d.DepartmentID IS NULL;
Output:
| EmployeeID | EmployeeName | DepartmentID | DepartmentName |
|---|---|---|---|
| NULL | NULL | 103 | Finance |
| NULL | NULL | 104 | Marketing |
| 4 | Michael | 106 | NULL |
Explanation:
e.EmployeeID IS NULLidentifies departments without matching employees.d.DepartmentID IS NULLidentifies employees without matching departments.- The OR operator returns unmatched records from either table.
This technique is useful when comparing related tables and finding missing relationships.
SQL Server FULL OUTER JOIN with Multiple Tables
SQL Server allows you to combine multiple tables using FULL OUTER JOIN.
Suppose we have three tables:
- Employees
- Departments
- Locations
The following example joins employee and department information, then combines the result with location information.
SELECT
e.EmployeeName,
d.DepartmentName,
l.LocationName
FROM Employees AS e
FULL OUTER JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID
FULL OUTER JOIN Locations AS l
ON d.LocationID = l.LocationID;
Explanation:
- The first FULL OUTER JOIN returns all employee and department records.
- The second FULL OUTER JOIN combines the intermediate result with Locations.
- Unmatched records from each joined input can remain in the final result.
The result depends on the relationships and matching conditions between the tables. When joining multiple tables, carefully define each ON condition to avoid unintended rows.
SQL Server FULL OUTER JOIN with GROUP BY
You can use FULL OUTER JOIN with GROUP BY to summarize matching and unmatched records.
For example, the following query counts employees for each department.
SELECT
d.DepartmentName,
COUNT(e.EmployeeID) AS TotalEmployees
FROM Employees AS e
FULL OUTER JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID
GROUP BY d.DepartmentName;
This query returns department groups, including departments without employees. The COUNT function ignores NULL employee IDs, so departments without matching employees have a count of zero.
However, employees without a matching department are grouped under a NULL department name.
To display unmatched employee records separately, you can include an additional grouping column:
SELECT
d.DepartmentID,
d.DepartmentName,
COUNT(e.EmployeeID) AS TotalEmployees
FROM Employees AS e
FULL OUTER JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID
GROUP BY
d.DepartmentID,
d.DepartmentName;
SQL Server FULL OUTER JOIN with ORDER BY
The ORDER BY clause sorts the records returned by FULL OUTER JOIN.
SELECT
e.EmployeeID,
e.EmployeeName,
d.DepartmentID,
d.DepartmentName
FROM Employees AS e
FULL OUTER JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID
ORDER BY
d.DepartmentID,
e.EmployeeID;
This query sorts the result by DepartmentID and then EmployeeID.
You can also use descending order:
SELECT
e.EmployeeName,
d.DepartmentName
FROM Employees AS e
FULL OUTER JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID
ORDER BY d.DepartmentName DESC;
SQL Server FULL OUTER JOIN with Aggregate Functions
Aggregate functions can be used with FULL OUTER JOIN to calculate summary information from both tables.
For example, the following query counts matching employees and displays the total number of records in each department group.
SELECT
d.DepartmentName,
COUNT(e.EmployeeID) AS TotalEmployees,
MAX(e.EmployeeID) AS HighestEmployeeID
FROM Employees AS e
FULL OUTER JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID
GROUP BY d.DepartmentName;
You can use aggregate functions such as COUNT(), SUM(), AVG(), MIN(), and MAX() with FULL OUTER JOIN when they are appropriate for your data.
SQL Server FULL OUTER JOIN with Subquery
A subquery can be used with FULL OUTER JOIN to join a filtered result set with another table.
For example, the following query retrieves employees whose EmployeeID is greater than 2 and combines them with all department records.
SELECT
e.EmployeeName,
d.DepartmentName
FROM
(
SELECT
EmployeeID,
EmployeeName,
DepartmentID
FROM Employees
WHERE EmployeeID > 2
) AS e
FULL OUTER JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID;
Explanation:
- The subquery filters the Employees table.
- FULL OUTER JOIN combines the filtered employee records with all departments.
- Unmatched employee and department records are also included.
- NULL values appear where a matching record does not exist.
SQL Server FULL OUTER JOIN vs INNER JOIN
FULL OUTER JOIN and INNER JOIN return different results.
| Feature | INNER JOIN | FULL OUTER JOIN |
|---|---|---|
| Matching records | Included | Included |
| Unmatched left-table records | Excluded | Included |
| Unmatched right-table records | Excluded | Included |
| NULL values for missing matches | Not returned for unmatched rows | Returned |
| Main purpose | Retrieve matching records | Retrieve all records from both tables |
For example, INNER JOIN returns only employees with matching departments. FULL OUTER JOIN also includes employees without departments and departments without employees.
SQL Server FULL OUTER JOIN vs LEFT JOIN and RIGHT JOIN
SQL Server provides three common outer join operations: LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN.
| JOIN Type | Records Returned |
|---|---|
| LEFT JOIN | All records from the left table and matching records from the right table |
| RIGHT JOIN | All records from the right table and matching records from the left table |
| FULL OUTER JOIN | All records from both tables, including unmatched records |
The following query demonstrates how FULL OUTER JOIN can be used to retrieve all records from both tables.
SELECT
e.EmployeeName,
d.DepartmentName
FROM Employees AS e
FULL OUTER JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID;
Use FULL OUTER JOIN when you need to compare two datasets and retain records that exist in either table.
Practical SQL Server FULL OUTER JOIN Query
The following complete example creates two tables, inserts sample records, and demonstrates FULL OUTER JOIN.
CREATE TABLE Departments
(
DepartmentID INT PRIMARY KEY,
DepartmentName VARCHAR(50)
);
CREATE TABLE Employees
(
EmployeeID INT PRIMARY KEY,
EmployeeName VARCHAR(50),
DepartmentID INT
);
INSERT INTO Departments (DepartmentID, DepartmentName)
VALUES
(101, 'IT'),
(102, 'HR'),
(103, 'Finance'),
(104, 'Marketing');
INSERT INTO Employees (EmployeeID, EmployeeName, DepartmentID)
VALUES
(1, 'John', 101),
(2, 'David', 102),
(3, 'Sarah', 101),
(4, 'Michael', 106);
SELECT
e.EmployeeID,
e.EmployeeName,
d.DepartmentID,
d.DepartmentName
FROM Employees AS e
FULL OUTER JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID
ORDER BY
COALESCE(d.DepartmentID, e.DepartmentID);
Expected result:
| EmployeeID | EmployeeName | DepartmentID | DepartmentName |
|---|---|---|---|
| 1 | John | 101 | IT |
| 3 | Sarah | 101 | IT |
| 2 | David | 102 | HR |
| NULL | NULL | 103 | Finance |
| NULL | NULL | 104 | Marketing |
| 4 | Michael | NULL | NULL |
The final query uses COALESCE() to sort records using the department ID from either table. It returns all matching and unmatched records from both tables.
This example demonstrates how the SQL Server FULL OUTER JOIN clause works with real database tables.