SQL Right Jion

SQL RIGHT JOIN returns all the values from the right table and it also includes the records matching from left table. The result will be NULL if no matching in left table.

The syntax for the RIGHT JOIN is

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;

                                                                 Or

SELECT column_name(s)
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name=table2.column_name;

 

Example:

Let us consider two tables

1.Employee table

Id Employee_Name Empl_Age Monthly package
1 Sushil 22 18000
2 Bhargav 32 45000
3 Mohanthy 26 25000
4 Nithin 38 52000

 

 

2.Payment Table

Payment_ID Date Employee_Id Amount
150 23-02-2016 1 2500
151 30-06-2015 2 5000
152 14-09-2016 4 6500

 

 

Now we RIGHT JOIN these tables

SELECT ID, Employee_Name, Amount, Date
FROM Employee
RIGHT JOIN Payment
ON Employee.ID=Payment.Employee_Id;

 

The result will be displayed as follows

Employee_ID Employee_Name Amount Date
1 Sushil 2500 23-02-2016
2 Bhargav 5000 30-06-2015
4 Nithin 6500 14-09-2016