Performing Right Joins in SQL

A RIGHT JOIN in SQL is used to retrieve records from two tables, ensuring that all rows from the right table are included, even if there are no matching records in the left table. In other words, it returns all rows from the right table, and the matched rows from the left table. If there is no match, NULL values are returned for columns from the left table.

Right joins are the mirror image of left joins. While a left join returns all records from the left table, a right join returns all records from the right table and the matching rows from the left.

Syntax of RIGHT JOIN

The basic syntax of the RIGHT JOIN is as follows:

				
					SELECT column_list
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;

				
			
  • table1: The left table.
  • table2: The right table.
  • column_list: The columns you wish to select.
  • The join condition is specified using the ON clause, which matches rows based on a common field.

How RIGHT JOIN Works

Consider two tables: employees and departments.

employees Table

employee_idnamedepartment_id
1Alice10
2Bob20
3Charlie30
4DavidNULL

departments Table

department_iddepartment_name
10Sales
20HR
30Engineering
40Marketing

To perform a Right Join to see all departments, even those that have no employees, we write:

				
					SELECT employees.name, departments.department_name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.department_id;

				
			

Explanation

  • We are joining the employees table on the departments table based on the department_id.
  • The query returns all rows from the departments table, regardless of whether there is a matching employee in the employees table.

Output

namedepartment_name
AliceSales
BobHR
CharlieEngineering
DavidMarketing

In this case, all the employees have a department assigned, so no NULL values are returned. Now let’s modify the employees table and see what happens when some employees are not assigned to a department.

Modified employees Table

employee_idnamedepartment_id
1Alice10
2Bob20
3CharlieNULL
4DavidNULL

Now, we run the same RIGHT JOIN query.

Output with NULL values

namedepartment_name
AliceSales
BobHR
NULLEngineering
NULLMarketing
  • The RIGHT JOIN ensures that all departments are returned.
  • Since Charlie and David are not assigned to a department (NULL values in department_id), no employee names are returned for Engineering and Marketing, and instead, NULL values are shown.

RIGHT JOIN vs LEFT JOIN

A common question is how RIGHT JOIN differs from LEFT JOIN. Let’s compare the two.

  • LEFT JOIN: Returns all rows from the left table and matched rows from the right table. Unmatched rows from the right table will return NULL values.
  • RIGHT JOIN: Returns all rows from the right table and matched rows from the left table. Unmatched rows from the left table will return NULL values.

The difference lies in which table’s rows are prioritized for inclusion in the result set. If you switch the tables in a LEFT JOIN, you get the effect of a RIGHT JOIN, and vice versa.

Example with LEFT JOIN (for comparison)

				
					SELECT employees.name, departments.department_name
FROM departments
LEFT JOIN employees
ON departments.department_id = employees.department_id;

				
			

This will give the same result as the RIGHT JOIN example but the tables have switched places.

Using WHERE Clause with RIGHT JOIN

You can add filtering conditions using a WHERE clause when performing a right join. For example, to retrieve only those departments where there are no employees, you can do this:

				
					SELECT employees.name, departments.department_name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.department_id
WHERE employees.name IS NULL;

				
			

Output

namedepartment_name
NULLEngineering
NULLMarketing

This query retrieves all departments without any employees by filtering out NULL values for the employee names.

RIGHT JOIN with Multiple Tables

You can use a RIGHT JOIN with multiple tables to query more complex data structures. Suppose we have another table, projects, with details of employees working on different projects:

employee_idnamedepartment_id
1Alice10
2Bob20
3Charlie30
4DavidNULL
department_iddepartment_name
10Sales
20HR
30Engineering
40Marketing

projects Table

project_idproject_nameemployee_id
101Project Alpha1
102Project Beta2
103Project Gamma3

In this case:

  • The projects table includes a project_id (unique identifier for each project), project_name (name of the project), and employee_id (which links the project to the employee working on it).
				
					SELECT employees.name, departments.department_name, projects.project_name
FROM employees
RIGHT JOIN departments ON employees.department_id = departments.department_id
RIGHT JOIN projects ON employees.employee_id = projects.employee_id;

				
			

Output

namedepartment_nameproject_name
AliceSalesProject Alpha
BobHRProject Beta
CharlieEngineeringProject Gamma
NULLMarketingNULL

Explanation of the output:

  • Alice, Bob, and Charlie have matching department_id and employee_id with their respective departments and projects, so their names, departments, and project names are shown.
  • Since the RIGHT JOIN ensures that all departments (and projects) will be shown even if there is no employee associated, we see a NULL for the employee in the Marketing department because no employee is assigned to it. Similarly, there’s no project for this department, so NULL appears for the project_name.

If RIGHT JOIN is used with more tables or if the tables have more or fewer rows, the output may vary accordingly.

RIGHT JOIN is a valuable SQL tool when you need to retrieve all records from the right table and their matching records from the left table. It’s the opposite of the left join, providing flexibility in how you manage and query data across related tables. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India