-1

I have been asked in an interview that why we need left and right join separately while we can get same result by using any of them by changing the order of table? Please help me.

Sanjay Gupta
  • 31
  • 1
  • 8
  • You don't need both -- but would you rather make 3 lefts when 1 right turn can do? Just kidding of course, it's just standard sql. To each his own -- I prefer `left join` myself though. Others think "backwards" and prefer `right join`... – sgeddes Dec 10 '15 at 02:53

2 Answers2

2

Technically, the language doesn't need both. In fact, I avoid right join and see no issues at all. Left join is preferable for two reasons. First, the logic is simpler to me -- keep all rows in the first table and then matching rows in the second and subsequent tables. More importantly, SQL queries are parsed left to right so:

from a left join b left join c

is parsed as

from (a left join b) left join c

If you work out some complex examples, you will see that this structure of parsing is more intuitive with left join than right join.

Of course, databases support both forms because the ANSI standard requires it. And, if some people have different preferences, they can use right join as much as they want.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
2

If you only have 2 tables to join, then yes, you will not see the difference at all.

But for 3 or more tables, the order of the tables being joined matters.

Check this question to see examples of it.

Community
  • 1
  • 1
KaeL
  • 3,639
  • 2
  • 28
  • 56