why we have need of left and right join

橙三吉。 提交于 2020-01-17 13:42:45

问题


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.


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/34192816/why-we-have-need-of-left-and-right-join

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!