The RIGHT JOIN keyword returns all records from the right table (table2), and the matching records from the left table (table1).
In a SQL right Outer Join, we get following rows in our output. It gives the output of the matching row between both the tables. If no records match from the right table, it also shows those records with NULL values.
Syntax:SELECT column_name(s)
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name = table2.column_name;
Example:
Lets take below two tables
Employee
EmpName | DeptID |
Rafferty | 31 |
Jones | 33 |
Heisenberg | 33 |
Robinson | 34 |
Smith | 34 |
Williams | null |
Department
DeptID | DepName |
31 | Sales |
33 | Engineering |
34 | Clerical |
35 | Marketing |
SQL Query:
SELECT e.EmpName, d.DepName
FROM Employees AS e
RIGHT OUTER JOIN Department AS d ON e.DepID = d.DepID;
Output:
EmpName |
DepName |
Rafferty |
Sales |
Heisenberg |
Engineering |
Jones |
Engineering |
Smith |
Clerical |
Robinson |
Clerical |
null |
Marketing |
Please visit related article below:
No comments:
Post a Comment