In SQL, joins are used to combine data from multiple tables based on related columns. One important type of join is the RIGHT JOIN, also known as RIGHT OUTER JOIN. It retrieves all records from the right table and the matching records from the left table. If there is no match in the left table, NULL
values will appear in the result for columns from the left table.
A RIGHT JOIN (or RIGHT OUTER JOIN) returns all rows from the right table, even if there is no corresponding match in the left table. If a record in the right table does not have a match in the left table, the query will return NULL
for all columns of the left table.
In simpler terms:
NULL
for the left table’s columns.The syntax for a RIGHT JOIN is very similar to other types of joins:
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;
Let’s understand how a RIGHT JOIN works through an example.
Consider two tables:
-- employees table
+-------------+-----------------+---------------+
| employee_id | employee_name | department_id |
+-------------+-----------------+---------------+
| 1 | Alice | 1 |
| 2 | Bob | 2 |
| 3 | Charlie | NULL |
+-------------+-----------------+---------------+
-- departments table
+---------------+--------------------+
| department_id | department_name |
+---------------+--------------------+
| 1 | HR |
| 2 | Finance |
| 3 | Engineering |
+---------------+--------------------+
RIGHT JOIN Example:
SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.department_id;
Explanation:
employee_id
, employee_name
, and department_name
.employees
table and the departments
table using the department_id
column.employee_name
will be NULL
.Result:
employee_id | employee_name | department_name |
---|---|---|
1 | Alice | HR |
2 | Bob | Finance |
NULL | NULL | Engineering |
Explanation of Result:
employee_name
values are displayed.employee_id
and employee_name
are NULL
.You can use a RIGHT JOIN to filter results in a query. For example, you can find departments that do not have any employees assigned to them.
SELECT departments.department_name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.department_id
WHERE employees.employee_name IS NULL;
Explanation:
NULL
in the employee_name
column.WHERE
clause ensures that only departments without any employees are returned.Result:
department_name |
---|
Engineering |
You might wonder how a RIGHT JOIN compares to a LEFT JOIN.
NULL
for columns from the right table.NULL
for columns from the left table.
SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;
Result for LEFT JOIN:
employee_id | employee_name | department_name |
---|---|---|
1 | Alice | HR |
2 | Bob | Finance |
3 | Charlie | NULL |
Explanation:
NULL
for department_name
.NULL
in the department_name
) is returned, while in the RIGHT JOIN, Engineering (a department with no employees) is included with NULL
values for the employee columns.Just like other joins, you can use the RIGHT JOIN to combine data from multiple tables. For example, consider a projects
table that stores the projects assigned to employees.
-- projects table
+------------+-------------+----------------+
| project_id | project_name| employee_id |
+------------+-------------+----------------+
| 1 | Project X | 1 |
| 2 | Project Y | 2 |
| 3 | Project Z | NULL |
+------------+-------------+----------------+
You can join the employees, departments, and projects tables using RIGHT JOIN:
SELECT employees.employee_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;
Result:
employee_name | department_name | project_name |
---|---|---|
Alice | HR | Project X |
Bob | Finance | Project Y |
NULL | NULL | Project Z |
Explanation:
NULL
is displayed in the employee_name
and department_name
columns.ON
clause are indexed to improve performance, particularly for large tables.WHERE
clauses to limit the data being processed when using RIGHT JOIN.