question

JulieJiji avatar image
JulieJiji asked

Left join and Right join works same but why do we have both?

We have left join and right join in sql server and both works in same way in logical. But why we have both join vailable.? What is the difference in parsing these two joins.? In which case we will prefer right join?When it slow down the query? I expect an excellent way of reply.. Thanking you in anticipation..
tsqljoinsparsingleft
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sure thing. Just remember that the site works off of votes. Any question you ask, and any you look at, you should vote good answers up by clicking on the thumbs up next to them. If it's your question, you can also select the best answer by clicking on the check box next to one answer.
0 Likes 0 ·

1 Answer

·
Grant Fritchey avatar image
Grant Fritchey answered
Left and right joins are functionally equivalent. There is no difference between them beyond a difference in the application of the syntax. On a right join, all values from the right table are returned and only those values that match on the left. In a left join, all values from the left table are returned and only those values that match on the right. They are just two constructs of the same idea to allow you to logically write your code in a way that makes more sense to you in a given situation. Also, it allows you to quickly modify behavior without having to rearrange your T-SQL code. There is no reason to prefer one over the other. Neither will slow the query or speed the query more than the other.
6 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

While a RIGHT JOIN can be constructed to return the same results as a LEFT JOIN, as Grant pointed out, they are different and do not return the same results. You are better off sticking with all LEFT JOINs -- resist the temptation to mix LEFT and RIGHT -- anyone maintaining your code will thank you. If at all possible stick with INNER joins on indexed columns, when your tables get into the millions of rows LEFT joins really drag down performance.
2 Likes 2 ·
Grant Fritchey, Thank you so much for the reply
0 Likes 0 ·
Both right join and left join gives the same result .Then why microsoft has given both?they can have one instead of two.So there should be some reason to have both.Somebody please correct me if it is wrong
0 Likes 0 ·
Microsoft hasn't given both per se. The ANSI Standard defines it and Microsoft implements it in their version of SQL. Like so many things in life there's more than one way to do things.
1 Like 1 ·
Think of it as syntactic sugar. It gives you more flexibility in how you display the code, which can matter in ease of writing and ease of maintenance.
0 Likes 0 ·
Thank yu all!!
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.