The RIGHT JOIN keyword
returns all the rows from the right table (tablename2), even if there are no
matches in the left table (tablename1).
Syntax:
SELECT column_name(s) FROM
tablename1 RIGHT JOIN tablename2
ON tablename1.column_name=tablename2.column_name
ON tablename1.column_name=tablename2.column_name
Note: In some databases
RIGHT JOIN is called RIGHT OUTER JOIN.
For Example,
The "Customer"
table:
PId
|
Name
|
Address
|
City
|
1
|
Vaishnavi
|
Anna Nagar
|
Bangalore
|
2
|
Shajil
|
T Nagar
|
Chennai
|
3
|
Vinoth
|
Shivaji St, T Nagar
|
Chennai
|
The "Orders" table:
O_Id
|
OrderNo
|
PId
|
1
|
77895
|
3
|
2
|
44678
|
3
|
3
|
22456
|
1
|
4
|
24562
|
1
|
5
|
34764
|
15
|
Query like,
SELECT Customer.Name, Customer.City, Orders.OrderNo FROM Customer
RIGHT JOIN Orders ON Customer.PId=Orders.PId ORDER BY Customer.Name
RIGHT JOIN Orders ON Customer.PId=Orders.PId ORDER BY Customer.Name
The result-set:
LastName
|
City
|
OrderNo
|
Vaishnavi
|
Bangalore
|
22456
|
Vaishnavi
|
Bangalore
|
24562
|
Vinoth
|
Chennai
|
77895
|
Vinoth
|
Chennai
|
44678
|
|
|
34764
|
The RIGHT JOIN keyword
returns all the rows from the right table (Orders), even if there are no
matches in the left table (Customer).
No comments:
Post a Comment