Introduction
SQL Server RIGHT JOIN clause is used to retrieve all records from the right table and matching records from the left table. When a record in the right table does not have a matching record in the left table, SQL Server returns NULL values for the columns from the left table.
The RIGHT JOIN is also known as the RIGHT OUTER JOIN. Both terms work in the same way in SQL Server.
In this tutorial, you will learn how to use the SQL Server RIGHT JOIN clause with practical examples, syntax, multiple table joins, filtering conditions, aggregate functions, and other SQL queries.
SQL Server RIGHT JOIN Syntax
The following is the basic syntax of the RIGHT JOIN clause in SQL Server:
SELECT
column_name1,
column_name2
FROM
left_table
RIGHT JOIN
right_table
ON
left_table.common_column = right_table.common_column;
Syntax explanation:
SELECT: Specifies the columns to retrieve.FROM: Defines the left table.RIGHT JOIN: Returns all records from the right table.ON: Specifies the matching condition between the tables.left_table: The table written before RIGHT JOIN.right_table: The table written after RIGHT JOIN.
The important point is that RIGHT JOIN preserves every row from the right-side table, regardless of whether a matching record exists in the left-side table.
SQL Server RIGHT JOIN Example
Let’s understand how the RIGHT 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 | 103 |
Departments Table
| DepartmentID | DepartmentName |
|---|---|
| 101 | IT |
| 102 | HR |
| 103 | Finance |
| 104 | Marketing |
| 105 | Sales |
The Employees table contains four employees, while the Departments table contains five departments. The Marketing and Sales departments do not have any employees.
Now, let’s retrieve all departments and their matching employees using RIGHT JOIN.
SELECT
Employees.EmployeeID,
Employees.EmployeeName,
Departments.DepartmentName
FROM Employees
RIGHT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
Output:
| EmployeeID | EmployeeName | DepartmentName |
|---|---|---|
| 1 | John | IT |
| 3 | Sarah | IT |
| 2 | David | HR |
| 4 | Michael | Finance |
| NULL | NULL | Marketing |
| NULL | NULL | Sales |
Explanation:
- All departments are returned because
Departmentsis the right table. - Matching employee records are displayed for IT, HR, and Finance.
- Marketing and Sales have no matching employees.
- SQL Server returns NULL for employee columns where no matching record exists.
This example demonstrates how RIGHT JOIN retrieves all records from the right table, including unmatched records.
SQL Server RIGHT OUTER JOIN
SQL Server supports both RIGHT JOIN and RIGHT OUTER JOIN. They are equivalent and produce the same result.
SELECT
Employees.EmployeeName,
Departments.DepartmentName
FROM Employees
RIGHT OUTER JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
You can use either syntax depending on your coding preference. The OUTER keyword is optional.
SQL Server RIGHT JOIN with Table Aliases
Table aliases make SQL queries shorter and easier to read, especially when joining multiple tables.
SELECT
e.EmployeeID,
e.EmployeeName,
d.DepartmentName
FROM Employees AS e
RIGHT JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID;
Explanation:
eis the alias for the Employees table.dis the alias for the Departments table.- The ON condition matches employees with their departments.
Aliases are particularly useful when working with large database tables and complex queries.
SQL Server RIGHT JOIN with WHERE Clause
You can use the WHERE clause with RIGHT JOIN to filter the returned records.
For example, the following query retrieves all departments and their employees but only displays departments with the name IT or Marketing.
SELECT
e.EmployeeName,
d.DepartmentName
FROM Employees AS e
RIGHT JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID
WHERE d.DepartmentName IN ('IT', 'Marketing');
Output:
| EmployeeName | DepartmentName |
|---|---|
| John | IT |
| Sarah | IT |
| NULL | Marketing |
The WHERE clause filters the result based on the department name. Marketing remains in the result because it is a row from the right table.
SQL Server RIGHT JOIN with IS NULL
You can use RIGHT JOIN with IS NULL to find records in the right table that do not have matching records in the left table.
For example, the following query finds departments that do not have any employees.
SELECT
d.DepartmentID,
d.DepartmentName
FROM Employees AS e
RIGHT JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID
WHERE e.EmployeeID IS NULL;
Output:
| DepartmentID | DepartmentName |
|---|---|
| 104 | Marketing |
| 105 | Sales |
Explanation:
The query returns only departments without matching employee records. Since unmatched left-table columns contain NULL, the IS NULL condition identifies those departments.
SQL Server RIGHT JOIN with Multiple Tables
SQL Server allows you to combine multiple tables using RIGHT JOIN and other JOIN operations.
Suppose we have three tables:
- Employees
- Departments
- Locations
The following query retrieves department information, employee details, and location information.
SELECT
e.EmployeeName,
d.DepartmentName,
l.LocationName
FROM Employees AS e
RIGHT JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID
LEFT JOIN Locations AS l
ON d.LocationID = l.LocationID;
Explanation:
- The RIGHT JOIN preserves all departments.
- Matching employee information is retrieved from the Employees table.
- The LEFT JOIN retrieves location information for each department.
- Departments without employees remain in the result.
When joining multiple tables, pay attention to the order of JOIN operations and the conditions used to connect each table.
SQL Server RIGHT JOIN with GROUP BY
You can use RIGHT JOIN with GROUP BY to calculate the number of employees in each department.
SELECT
d.DepartmentName,
COUNT(e.EmployeeID) AS TotalEmployees
FROM Employees AS e
RIGHT JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID
GROUP BY d.DepartmentName;
Output:
| DepartmentName | TotalEmployees |
|---|---|
| IT | 2 |
| HR | 1 |
| Finance | 1 |
| Marketing | 0 |
| Sales | 0 |
The COUNT(e.EmployeeID) function counts matching employees. It returns zero for departments without employees because COUNT ignores NULL values.
SQL Server RIGHT JOIN with ORDER BY
The ORDER BY clause sorts the result returned by RIGHT JOIN.
SELECT
e.EmployeeName,
d.DepartmentName
FROM Employees AS e
RIGHT JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID
ORDER BY d.DepartmentName ASC;
This query sorts the result alphabetically by department name.
You can also sort the results in descending order:
SELECT
e.EmployeeName,
d.DepartmentName
FROM Employees AS e
RIGHT JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID
ORDER BY d.DepartmentName DESC;
SQL Server RIGHT JOIN with Aggregate Functions
Aggregate functions can be used with RIGHT JOIN to calculate summary information from related tables.
For example, the following query counts employees in each department and calculates the total number of employees.
SELECT
d.DepartmentName,
COUNT(e.EmployeeID) AS TotalEmployees,
MAX(e.EmployeeID) AS HighestEmployeeID
FROM Employees AS e
RIGHT JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID
GROUP BY d.DepartmentName;
This query returns every department, even when no employees are assigned to it.
You can use aggregate functions such as COUNT(), SUM(), AVG(), MIN(), and MAX() with RIGHT JOIN when the data structure and query requirements support them.
SQL Server RIGHT JOIN with Subquery
A subquery can also be used with RIGHT JOIN to retrieve data from a filtered result set.
For example, the following query joins departments with employees whose EmployeeID is greater than 2.
SELECT
e.EmployeeName,
d.DepartmentName
FROM
(
SELECT EmployeeID, EmployeeName, DepartmentID
FROM Employees
WHERE EmployeeID > 2
) AS e
RIGHT JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID;
Explanation:
The subquery first filters the Employees table. The RIGHT JOIN then preserves all departments and displays matching employees from the filtered result.
Departments without matching employees still appear with NULL employee values.
SQL Server RIGHT JOIN vs LEFT JOIN
RIGHT JOIN and LEFT JOIN are both outer joins, but they preserve different sides of the joined tables.
| Feature | LEFT JOIN | RIGHT JOIN |
|---|---|---|
| Preserves all records from | Left table | Right table |
| Matching records | Included | Included |
| Unmatched preserved-side records | Included | Included |
| Missing opposite-side values | NULL | NULL |
| OUTER keyword | Optional | Optional |
For example, LEFT JOIN preserves all rows from the Employees table, while RIGHT JOIN preserves all rows from the Departments table in the earlier examples.
You can often rewrite a RIGHT JOIN as a LEFT JOIN by reversing the table order.
SELECT
e.EmployeeName,
d.DepartmentName
FROM Departments AS d
LEFT JOIN Employees AS e
ON d.DepartmentID = e.DepartmentID;
This query returns the same matching and unmatched department records as the earlier RIGHT JOIN query.
Practical SQL Server RIGHT JOIN Query
The following complete example creates two tables and demonstrates how RIGHT JOIN retrieves all departments, including those without employees.
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'),
(105, 'Sales');
INSERT INTO Employees (EmployeeID, EmployeeName, DepartmentID)
VALUES
(1, 'John', 101),
(2, 'David', 102),
(3, 'Sarah', 101),
(4, 'Michael', 103);
SELECT
e.EmployeeID,
e.EmployeeName,
d.DepartmentID,
d.DepartmentName
FROM Employees AS e
RIGHT JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID
ORDER BY d.DepartmentID;
The result contains all five departments. Departments with employees display their matching employee information, while departments without employees display NULL in the employee columns.
This practical example demonstrates the complete working process of the SQL Server RIGHT JOIN clause.