The RIGHT JOIN keyword in SQL returns the all matching records(or rows) and the records(or rows) which are present in the right table but not in the left table .That means that, if a certain row is present in the right table but not in the left, the result will include this row but with a NULL value in each column from the left . If a record from the left table is not in the right, it will not be included in the result.
The syntax for a RIGHT JOIN is :-
SELECT column_name(s) FROM tableA RIGHT JOIN tableB ON tableA.column_name = tableB.column_name;
SQL RIGHT JOIN EXAMPLE :
In this example we will consider two tables employee table containing details of the employees working in the particular department the and department table containing the details of the department
employee table :
department table :
dept_no d_name location D1 IT Delhi D2 HR Hyderabad D3 Finance Pune D4 Testing Noida D5 Marketing Mathura
To perform right- join on these two tables we will use the following SQL query:
select emp_no , emp_name ,d_name, location from employee right join dept on employee.dept_no = department.dept_no;
The output that we will get is as follows :
emp_no emp_name d_name location E1 Varun Singhal IT Delhi E2 Amrita Aggarwal HR Hyderabad E3 Ravi Anand Finance Pune [NULL] [NULL] Testing Noida [NULL] [NULL] Marketing Mathura
As right join gives the matching rows and the rows that are present in the left table but not in the right table .Here in this example ,we see that the department that contains no employee contains [NULL] values of emp_no and emp_name after performing the right join.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.