SQL Server FULL OUTER JOIN
Last Updated :
28 Dec, 2023
Joins in SQL are used to retrieve data from multiple tables based on a related column (or common column) between them. In this article, we will learn how to use FULL OUTER JOIN, which returns all rows from both tables being joined. It combines the results of both LEFT OUTER JOIN and RIGHT OUTER JOIN. Instead of discarding a row with no match, it fills NULL in the cells and returns all the rows (or tuples) from the left and right tables.
FULL OUTER JOIN
To understand FULL OUTER JOIN, you must be familiar with SQL, relational databases, and how to relate two or more tables. Before jumping on the main topic, we have explained how INNER JOIN, LEFT OUTER JOIN, and RIGHT OUTER JOIN works. If you're familiar with these terms, we encourage you to read this article from the very beginning.
Now let us understand how Full Outer Join works in MS SQL Server:
The syntax for a SQL Server FULL OUTER JOIN is as follows:
SELECT table1.column1, table1.column2, table2.column3, table2.column4, ...
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name;
Here,
- table1.column1, table1.column2, table2.column3, table2.column4, ...: are the columns we want to retrieve from the tables.
- table1: The left table.
- table2: The right table.
- ON table1.column_name = table2.column_name: is the condition for joining the tables based on a common column.
The result of a FULL OUTER JOIN includes all rows from both tables, matched where possible and with NULLs in the columns where no match is found.
Note:
- To perform any type of join, we must have a common column in both tables. The column name can be different, but the data type should be the same
- For the sake of our understanding, we treat the first table as the left table and the second table as the right table that are being joined.
Now let us understand different types of joins:
As we talked about, joins in SQL are used to retrieve data from multiple tables based on a related column. Let's have a quick look over INNER, LEFT, and RIGHT OUTER JOINS.
1. INNER JOIN
In a INNER JOIN operation, the SQL server returns only those rows that match the record from the left and the right table. If there is no match, it returns NULL.
Syntax:
SELECT * FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;

In the above diagram, only two rows were returned, which have matching records in the right table. To learn more about INNER JOIN refer to this article.
2. LEFT OUTER JOIN
In a Left Outer Join, the SQL server returns all the rows from the left table (table 1) and the matching records from the right table (table 2) are included. If there is no match in the right table, it fills NULL values in the columns of the right table.
Syntax:
SELECT * FROM table1
LEFT OUTER JOIN table2 ON table1.column_name = table2.column_name;

Instead of discarding the first row, the SQL server fills nulls in the column for the first row and returns all the rows of the left table. To learn more about SQL left join refer to this article.
3. RIGHT OUTER JOIN
In a Right Outer Join, the SQL server returns all the rows from the right table (table 2) and the matching records from the left table (table1) are included. If there is no match in the left table, it fills NULL values in the columns of the left table.
Syntax:
SELECT * FROM table1
RIGHT OUTER JOIN table2 ON table1.column_name = table2.column_name;

It also filled the NULL value for the third row and returned all rows from the right table with matching records from the left table. To learn more about RIGHT OUTER JOIN refer to this article.
4. FULL OUTER JOIN
FULL OUTER JOIN combines or performs union on the results of both LEFT OUTER JOIN and RIGHT OUTER JOIN.

Note: In the result of a FULL OUTER JOIN operation, the number of rows/tuples will be equal to the unique entry in the common column (or related column).
Implementation of FULL OUTER JOIN in MS SQL Server.
Let's consider two tables: Employees (Employee_id, Employee_name, Departement_Id) and Departments (Department_id, Department_name). The Employees table contains information about employees, while the Departments table contains information about different departments in a company.
We have inserted the following data in the Employee Table:
INSERT INTO EMPLOYEE (EMPLOYEE_ID, EMPLOYEE_NAME, DEPARTMENT_ID)
VALUES
(1, 'Priya', 2),
(2, 'Arun', 3),
(3, 'Aditya', 3),
(4, 'Rahul', 4),
(5, 'Akshay', 6);
Let's see the entries:
SELECT * FROM EMPLOYEE;
.png)
And following data in the Department Table:
INSERT INTO DEPARTMENT (DEPARTMENT_ID, DEPARTMENT_NAME)
VALUES
(2, 'Marketing'),
(3, 'IT Services'),
(4, 'Management'),
(5, 'HR');
Checking the entries:
SELECT * FROM DEPARTMENT;
.png)
Note: Here, we are not considering any referential integrity as we have not set the foreign key. The department_id column in Employee table can have an id that don't exist in the Department Table.
Full Outer Join is generally performed on common columns other than the foreign key.
Simple FULL OUTER JOIN:
SELECT Employee.Employee_ID, Employee.Employee_Name, Department.Department_ID, Department.Department_Name
FROM Employee
FULL OUTER JOIN Department
ON Employee.Department_ID = Department.Department_ID;
Output: When we run the above query, we get the following results.
.png)
In this example, we retrieve the Employee_ID and Employee_Name from the Employee table and the Department_Name and Department_ID from the Department table.
The result includes all employees, along with their respective departments, whether they are assigned to a department or not. As we can see Akshay do not belong to any department and the HR department has no employee.
Note: Here, the OUTER keyword is optional. We can simply use FULL JOIN instead of FULL OUTER JOIN. Both give us the same output.
Removing Outer Keyword:
SELECT *
FROM Employee
FULL JOIN Department
ON Employee.Department_ID = Department.Department_ID;
Result:
.png)
We get the same result.
Handling NULLs:
If we don't want to show NULL values in the result, we can handle it explicitly using the COALESCE() function. Here is how we can handle nulls:
SELECT COALESCE(Employee.Employee_Name, 'No Employee') AS Employee_Name,
COALESCE(Department.Department_Name, 'No Department') AS Department_Name
FROM Employee
FULL OUTER JOIN Department
ON Employee.Department_ID = Department.Department_ID;
Output: When we run the above query, we get the following results/rows.
.png)
In this example, we use the COALESCE function to handle or replace NULL values. If an employee is not assigned to a department or a department has no employees, the result displays "No Employee" or "No Department," respectively.
Conclusion
In this article, we look over INNER, LEFT, and RIGHT OUTER JOINs to build an intuition on how joins work. Later, we discussed FULL OUTER JOIN in depth with an example. We also learned how to handle nulls. Understanding FULL OUTER JOIN enhances your ability to write sophisticated SQL queries for diverse data scenarios. If you face any difficulty, you can pin down the same in the comment section.
Similar Reads
PostgreSQL - FULL OUTER JOIN
In PostgreSQL, the FULL OUTER JOIN is a powerful feature that combines the effects of both LEFT JOIN and RIGHT JOIN. This join operation retrieves all rows from both tables involved in the join, including unmatched rows from each table. For any unmatched rows, PostgreSQL fills the result with NULL v
4 min read
FULL OUTER JOIN in SQLite
In the area of data querying and manipulation, the ability to combine information from different sources is important. SQLite, a popular embedded database engine, offers a range of join operations to fast process. One such operation FULL OUTER JOIN is particularly powerful as it allows us to merge d
4 min read
PL/SQL Outer Join
In SQL, joins are used to retrieve data from two or more tables based on a related column. Joins can be categorized into different types: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, etc. In this article, we will learn about OUTER JOIN in PL/SQL, including its types, syntax, use cases, and ex
6 min read
SQL Server ALL Operator
In SQL Server the logical operators are used to perform conditional checks in SQL Queries. There are many different types of Logical operators like ANY, AND, LIKE, EXISTS, NOT, IN, and BETWEEN in SQL Server. These operators are very useful to filter and compare single or multiple data in SQL Queries
3 min read
SQL FULL JOIN
In SQL, the FULL JOIN (or FULL OUTER JOIN) is a powerful technique used to combine records from two or more tables. Unlike an INNER JOIN, which only returns rows where there are matches in both tables, a FULL JOIN retrieves all rows from both tables, filling in NULL values where matches do not exist
4 min read
PL/SQL Full Join
A FULL JOIN, also called a FULL OUTER JOIN, is a type of join in PL/SQL that returns all rows from both tables, even if there is no matching data in the other table. If a row from one table doesnât have a match in the other, it will still be included in the result, with NULL values filling in the mi
6 min read
Full join and Inner join in MS SQL Server
Full Join Full join selects all the rows from the left and right tables along with the matching rows as well. If there are no matching rows, it will be displayed as NULL. Syntax: select select_list from table1 full join table2 on join _predicate (OR) select table1.*, table2.* from table1 full join t
2 min read
SQL Server ALIASES
Aliases in SQL Server are the temporary names given to tables or columns to make it easy to read and maintain the data. Aliases help you to provide different names to columns and tables temporarily so that users can easily understand the data of the table and it does not change any data of the table
3 min read
Recursive Join in SQL
In SQL, a recursive join is a powerful technique used to handle hierarchical data relationships, such as managing employee-manager relationships, family trees, or any data with a self-referential structure. This type of join enables us to combine data from the same table repeatedly, accumulating rec
3 min read
SQL Full Outer Join Using Where Clause
SQL (Structured Query Language) provides powerful tools to manage and query relational databases. One of its most flexible features is the JOIN operation, which allows combining data from two or more tables based on related columns. Among the various types of joins, the FULL OUTER JOIN is particular
4 min read