SQL Right Join
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 :
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 :
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.