Account
Categories

RIGHT JOIN: Retrieve Unmatched Left Table Records


RIGHT JOIN is an SQL operator used to retrieve all records from the right table and only match records from the left table.

If no match is found in the left table, then it returns NULL for the left table’s columns. In this process, the columns act as operands

Syntax:

SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.common_column = table2.common_column;

Example:


Table1:  Employees

Employee_id	     Name	            Department_id

1	             Mohan	                   11
2	             Anaya	                   12
3	             Babita	                   13

Table2: Departments

Department_id	         Department_name

11	                              HR
12	                             IT
14	                            Finance


SQL Query:

SELECT Employees.name, Departments.department_name
FROM Employees
RIGHT JOIN Departments
ON Employees.department_id = Departments.department_id;

Output:

Name	        Department_name

Mohan	              HR
Anaya	              IT
NULL	          Finance