The SQL Right Join is a SQL Join Type used to return all the records present in the Right table and matching rows from the Left table.
The SQL Server Right Outer join can also be called Right Join. So, it is optional to use the Outer Keyword. Remember, All the Unmatched rows from the Left table will fill with NULL Values.
SQL RIGHT JOIN Syntax
The syntax of the Right Join in SQL Server is as follows:
-- SQL Server RIGHT JOIN SELECT Table1.Column(s), Table2.Column(s), FROM Table1 RIGHT 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 RIGHT JOIN Table2 ON Table1.Common_Column = Table2.Common_Column
Let us see the visual representation of the Sql Server Right Outer join for better understanding.
From the above image, you can understand easily that, Right Outer join displays all the records present in Table 2 and matching records from Table 1
For this Sql Server Right Join example, We are going to use two tables in our [SQL Server Tutorials] Database.
Data present in the Employee Table is:
Data present in the SQL Server Department Table is:
SQL Right Join Select All Columns
The following SQL Right Join Query will display all the columns present in the Department table, and matching records from the Employees table
-- SQL Server RIGHT JOIN Example SELECT * FROM [Employee] RIGHT OUTER JOIN [Department] ON [Employee].[DepartID] = [Department].[id]
OUTPUT
Right Join without Outer keyword
As we said before, it is optional to use an Outer keyword. Let me remove the Outer keyword, and work will RIGHT JOIN
-- SQL Server RIGHT JOIN Example SELECT * FROM [Employee] RIGHT OUTER JOIN [Department] ON [Employee].[DepartID] = [Department].[id]
OUTPUT
Although the Employee table has 15 records, Right join is displaying 13 records. It is because
- Department Id for 14th and 15th records in Employee table are NULLS, so there are no matching records in the right table.
- If you observe the 8th and 9th records, they are displaying NULL values. Because in the Employee table, there are no matching records for Department Id 3, 4 (Module Lead and Team Lead) in the Department table. So they are replaced by NULLS.
NOTE: The [Department ID] column is repeated twice, which is annoying to the user. By selecting individual column names we can avoid unwanted columns. So, please avoid SELECT * Statements in Right Join
SQL Right Join Select Few Columns
Please place the required columns after the SELECT Statement to avoid unwanted columns in Joins.
-- SQL Server RIGHT JOIN Example SELECT [FirstName] ,[LastName] ,[DepartmentName] FROM [Employee] RIGHT JOIN [Department] ON [Employee].[DepartID] = [Department].[id]
OUTPUT
The above Right Join query will excellently work as long as the column names from both Employee and Department tables are different like above. If they have the same Column names, you get an error. Let us see how to solve the issue.
Here, we used the above right outer join query. However, we added id from the department table as an additional column.
-- SQL Server RIGHT JOIN Example SELECT [FirstName] ,[LastName] ,id ,[DepartmentName] FROM [Employee] RIGHT OUTER JOIN [Department] ON [Employee].[DepartID] = [Department].[id]
As you see, it is throwing an error: Ambiguous column name id. It is because the id column is available in both Employee and department table. And SQL Server doesn’t recognize which column you are claiming.
To resolve this kind of concern, practice the table name before the column name. The following right outer join query is using the ALIAS table name before the column names.
By this approach, we can notify the Server that we are looking for id column belonging to the department table.
We can rewrite the earlier query as:
-- SQL Server RIGHT JOIN Example SELECT Emp.[FirstName] AS [First Name] ,Emp.[LastName] AS [Last Name] ,Dept.id ,Dept.[DepartmentName] AS [Department Name] FROM [Employee] AS Emp RIGHT JOIN [Department] AS Dept ON Emp.[DepartID] = Dept.[id]
OUTPUT
Sql Server Right Join Where Clause
The right outer join also allows us to use Where Clause to restrict the records returned by the right Join. In this example, we use WHERE Clause along with the Right Outer Join.
-- SQL Server RIGHT JOIN Example SELECT Emp.[FirstName] AS [First Name] ,Emp.[LastName] AS [Last Name] ,Dept.[DepartmentName] AS [Department Name] FROM [Employee] AS Emp RIGHT JOIN [Department] AS Dept ON Emp.[DepartID] = Dept.[id] WHERE Emp.[FirstName] IS NOT NULL
OUTPUT
SQL Right Join Order By Clause
The Right Join allows us to use Order By Clause in Right Join to rearrange the order of the records.
-- SQL Server RIGHT JOIN Example SELECT Emp.[FirstName] AS [First Name] ,Emp.[LastName] AS [Last Name] ,Dept.[DepartmentName] AS [Department Name] FROM [Employee] AS Emp RIGHT JOIN [Department] AS Dept ON Emp.[DepartID] = Dept.[id] ORDER BY [FirstName] ASC
OUTPUT