SQL FULL JOIN

The SQL Server Full Join type returns all the records (or rows) present in both the left and right tables. All the Unmatched rows fill with NULL Values.

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

SQL Server Full Join Diagramming representation

From the above image, you can easily understand that the SQL Server Full Join displays all the records present in Table 1 and Table 2. The syntax of the Full Join 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 will 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 Outer Join Select All Columns

The following example selects all columns query will display all the columns and rows 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 displays 17 records because there are two records in the Department, i.e., Department Id 3, 4 (Module Lead and Team Lead), 15 + 2 = 17 total records.
  • No matching records exist in the Employees for Department Id 3 and 4 (Module Lead and Team Lead), so NULLS replace them.
  • ID numbers 10, 11, 14, and 15 of [DepartID], id, [Department Name] display NULL Values. Because the Department Id for them in the Employee table is NULLS, there are no matching records in the right table.

SQL Server Full Join without Outer keyword

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

SELECT *
FROM [Employee]
 FULL JOIN
     [Department] ON
 [Employee].[DepartID] = [Department].[id]
without Outer keyword 7

NOTE: The [Department ID] column is repeated twice, which annoys 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 JOINSelect Few Columns
     [Department] ON
  [Employee].[DepartID] = [Department].[id]
Select Few Columns 4

The above query will work perfectly as long as the column names from both tables (Employee and Department) are different.

What happens if we have the same Column names in both 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 SQL Full Join query. Still, we added the 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 throws an error: Ambiguous column name id. It is because the id is present on both Employee and department tables. And SQL Server doesn’t know which one you want it to retrieve.

Ambiguous column name id Error 8

To resolve this issue, you always have to use the table name before the column name. For example, the following code uses the ALIAS table name before the column names. By this approach, we can inform the Server that we are looking for an 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]
Solve Ambiguous column name id Error using SQL Full Outer Join Alias name 9

SQL Server Full Outer Join in Where Clause

It also allows us to use the Where Clause to restrict the number of rows returned by this. Therefore, we will use the WHERE Clause and the Full Join in this example.

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 in Where Clause 10

SQL Full Outer Join Order By Clause

It allows us to use the 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 Order By Clause 6
Categories SQL