0

Possible Duplicate:
right join versus left join

is there any need of RIGHT join, bacause we can achieve the same result using LEFT join by just altering the table name

i have two tables Persons and Orders

P_Id    LastName    FirstName   Address City
1   Hansen  Ola Timoteivn 10    Sandnes
2   Svendson    Tove    Borgvn 23   Sandnes
3   Pettersen   Kari    Storgt 20   Stavanger

and

O_Id    OrderNo P_Id
1   77895   3
2   44678   3
3   22456   1
4   24562   1
5   34764   15

and i have a query for left join as:

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
LEFT JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName

and with right join:

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Orders
RIGHT JOIN Persons
ON Orders.P_Id=Persons.P_Id
ORDER BY Persons.LastName

both gives the same result.

Community
  • 1
  • 1
prakash
  • 1
  • 2
  • left and right joins are only easily interchangeable in the way you suggest if only two tables are involved in the query. [Otherwise the changes may need to be more extensive](http://stackoverflow.com/a/7313507/73226) – Martin Smith Jan 15 '13 at 11:50

2 Answers2

1

Both versions exist for convenience. Only one would be necessary, as you say.

However, sometimes a large query would have to be changed quite a bit if there were only one (RIGHT or LEFT) and you wanted to add a non-inner join to the query without changing it much.

With both as an option, it's usually easy to work a non-inner join into a pre-existing query without too much other reworking of the query.

At least that's the only time I find myself writing a RIGHT JOIN: when I already have a query and need to add in a non-inner join and making it a LEFT JOIN would cause me to rework the query more than I want to.

DWright
  • 9,258
  • 4
  • 36
  • 53
-1

Check this for more understanding

http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

Sandip Bantawa
  • 2,822
  • 4
  • 31
  • 47