SQL Right Join
Advertisements
Right Join in SQL
Right Join return all rows from the right table, and the matched rows from the left table.
The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match.
Syntax
SQL RIGHT JOIN Syntax SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name=table2.column_name;
or
Syntax
SELECT column_name(s) FROM table1 RIGHT OUTER JOIN table2 ON table1.column_name=table2.column_name;
Note: In some databases RIGHT JOIN is called RIGHT OUTER JOIN.
Employee table
Emp_id | Name | Age |
---|---|---|
101 | Amit Sukla | 24 |
102 | Rahul jain | 34 |
103 | Sultan Alam | 54 |
104 | Gaurav rawat | 26 |
105 | Hitesh Kumar | 35 |
Payment table
Payment_id | Date | Salary |
---|---|---|
101 | 20-01-2014 | 20000 |
102 | 23-03-2014 | 45000 |
103 | 12-02-2014 | 50000 |
104 | 28-07-2014 | 55000 |
105 | 20-11-2014 | 40000 |
After apply right join on these two table, result show like below;
Example
SELECT Employee.emp_id, Payment.Name FROM Employee RIGHT JOIN Payment ON Employee.emp_id=Payment.Payment_id ORDER BY Employee.emp_id;
In above syntax we select 2 columns, emp_id and name from table Employee and table Payment. And retrieving all rows where the employee identification number is the same in both the Employee and Payment tables.
Payment_id | Name |
---|---|
101 | Amit Sukla |
102 | Rahul jain |
103 | Sultan Alam |
104 | Gaurav rawat |
105 | Hitesh Kumar |
Google Advertisment