SQL FULL JOIN

The SQL Full Join type used to return all the records (or rows) present in both the Left table and the right table. All the Unmatched rows filled with NULL Values.

The SQL Full join can also call as Full outer Join. So it is optional to use the Outer Keyword. Let us see the visual representation of this for better understanding.

Diagramming representation of Full

From the above image, you can understand easily that the Full join displays all the records present in Table1 and Table2. The syntax of the Full Join in SQL Server is as shown below:

SELECT Table1.Column(s), Table2.Column(s),
FROM Table1
 FULL OUTER JOIN
     Table2 ON
   Table1.Common_Column = Table2.Common_Column

--OR We can Simply Write it as
SELECT Table1. Column(s), Table2. Column(s),
FROM Table1
 FULL JOIN
     Table2 ON
   Table1.Common_Column = Table2.Common_Column

For this SQL Full Join demonstration, We are going to use two tables (Employee and Department) present in our Database. Data present in the Employee is:

Employee Left Table 1

Data present in the SQL Server Department is:

Right Table 2

SQL Full Join Select All Columns

The following Sql Full Outer Join Select all columns query will display all the columns and rows present in Employees and Department.

SELECT *
FROM [Employee]
 FULL OUTER JOIN
     [Department] ON
 [Employee].[DepartID] = [Department].[id]
SQL FULL JOIN 3
  • If you observe the above screenshot, Although We have 15 records in the Employee, SQL Full Outer Join combines and it is displaying 17 records. It is because there are two records in the Department, i.e., Department Id 3, 4 (Module Lead and Team Lead), so 15 + 2 = 17 total records.
  • For Department Id 3 and 4 (Module Lead and Team Lead), there are no matching records in the Employees, so NULLS replaces them.
  • ID number 10, 11, 14 and 15 of [DepartID], id, [Department Name], it is displaying NULL Values. It is because the Department Id for them in the Employee table is NULLS, so there are no matching records in the right table.

Full without Outer keyword

As we said before, it is optional to use an Outer keyword in this Join type. Let me remove the Outer keyword, and work will SQL FULL JOIN

SELECT *
FROM [Employee]
 FULL JOIN
     [Department] ON
 [Employee].[DepartID] = [Department].[id]
SQL FULL JOIN 7

NOTE: The [Department ID] column is repeated twice, which is annoying to the user. By selecting individual column names we can avoid unwanted ones so, Please avoid SELECT * Statements.

SQL Full Join Select Few Columns

Please place the required fields after the SELECT Statement to avoid unwanted columns.

SELECT [FirstName]
      ,[LastName]
      ,[DepartmentName]
FROM [Employee]
  FULL JOIN
     [Department] ON
  [Employee].[DepartID] = [Department].[id]
SQL FULL JOIN 4

Above SQL Full Join query will perfectly work as long as the column names from both tables (Employee and Department) are different like above.

What happens if we have the same Column names in both the tables? Well, you will end up in a mess. Let us see how to resolve the issue. Before we get into the solution, let me show you one practical example.

As you can see, we are using the above query. Still, we added id from the department table as an additional column.

SELECT [FirstName]
      ,[LastName]
      ,id
      ,[DepartmentName]
FROM [Employee]
FULL OUTER JOIN
     [Department] ON
           [Employee].[DepartID] = [Department].[id]

As you can see joining tables is throwing an error: Ambiguous column name id. It is because the id is present in both Employee and department table. And SQL Server doesn’t know which one you are asking it to retrieve.

SQL FULL JOIN 8

To resolve this kind of issue, you always have to use the table name before the column name. The following code is using the ALIAS table name before the column names. By this approach, we can inform the SQL Server that we are looking for id belonging to the department table.

We can write the above query as:

SELECT Emp.[FirstName] AS [First Name]
      ,Emp.[LastName] AS [Last Name]
	  ,Dept.id 
      ,Dept.[DepartmentName] AS [Department Name]
FROM [Employee] AS Emp
FULL JOIN
     [Department] AS Dept ON
          Emp.[DepartID] = Dept.[id]
SQL FULL JOIN 9

Full in Where Clause

It also allows us to use Where Clause to restrict the number of rows returned by this. In this example, we will use that WHERE Clause along with the SQL Server Full Join.

SELECT Emp.[FirstName] AS [First Name]
      ,Emp.[LastName] AS [Last Name]
      ,Dept.[DepartmentName] AS [Department Name]
FROM [Employee] AS Emp
  FULL OUTER JOIN
     [Department] AS Dept ON
    Emp.[DepartID] = Dept.[id]
WHERE Dept.[DepartmentName] IS NOT NULL
SQL FULL JOIN 10

SQL Full outer Join Order By Clause

This allows us to use Order By Clause to rearrange the order of the records.

SELECT Emp.[FirstName] AS [First Name]
      ,Emp.[LastName] AS [Last Name]
      ,Dept.[DepartmentName] AS [Department Name]
FROM [Employee] AS Emp
  FULL OUTER JOIN
     [Department] AS Dept ON
    Emp.[DepartID] = Dept.[id]
ORDER BY [DepartmentName] ASC
SQL FULL JOIN 6