The RIGHT JOIN keyword returns all the records from the right table and matching records from the left table.
Syntax:
SELECT column_name(s) FROM right_table
INNER JOIN left_table
ON right_table.column_name = left_table.column_name;
RIGHT (OUTER) JOIN
DEMO TABLES
STUDENTS TABLE
Roll_No | Name | Marks |
1 | Dev | 35 |
2 | Ayush | 45 |
3 | Ram | 55 |
4 | Pyush | 65 |
5 | Nazim | 75 |
CLG TABLE
Id | Phone_no |
1 | 946547846 |
2 | 854547845 |
3 | 756547841 |
4 | 846447842 |
5 | 874647841 |
10 | 985647841 |
11 | 964747841 |
12 | 765447841 |
Example: To perform right join on both the above tables
SELECT Roll_No, Name FROM students
RIGHT JOIN clg
ON clg.Roll_No = students.Roll_No;
The resultant table will be as shown below:
Roll_No | Name | Marks | Id | Phone_no |
1 | Dev | 35 | 1 | 946547846 |
2 | Ayush | 45 | 2 | 854547845 |
3 | Ram | 55 | 3 | 756547841 |
4 | Pyush | 65 | 4 | 846447842 |
5 | Nazim | 75 | 5 | 874647841 |
NULL | NULL | NULL | 10 | 985647841 |
NULL | NULL | NULL | 11 | 964747841 |
NULL | NULL | NULL | 12 | 765447841 |