Why can't we use left outer join by swapping the tables, instead of Right outer Join?

后端 未结 2 706
执笔经年
执笔经年 2021-01-03 03:46

few days back, I have faced the question in an interview as following. \"what is Right outer join?\" I answered, \" Right outer join joins two tables and returns the matched

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-03 04:35

    This is an interesting article/discussion on the topic. From the answers I think the following sums it up rather well (by Jeremiah Peschka): Convenience, optimization and it being the ANSI standard.

    Convenience and optimization. Just because we can write our query as a LEFT OUTER JOIN, doesn’t mean that you should. SQL Server provides a RIGHT OUTER JOIN showplan operator (http://msdn.microsoft.com/en-us/library/ms190390.aspx). There are times when it’s going to be most efficient to use a right outer join. Leaving that option in the language 1) gives you the same functionality in the language that you have in the optimizer and 2) supports the ANSI SQL specification. There’s always a chance, in a sufficiently complex plan on a sufficiently overloaded SQL Server, that SQL Server may time out query compilation. In theory, if you specify RIGHT OUTER JOIN instead of a LEFT OUTER JOIN, your SQL could provide SQL Server with the hints it needs to create a better plan. If you ever see this situation, though, you should probably blog about it :)

    No programming task requires a join, but you can also write all of your queries using syntax like SELECT * FROM a, b, c, d WHERE (a.id = b.a_id OR b.a_id IS NULL) and still have perfectly valid, well-formed, and ANSI compliant SQL.

提交回复
热议问题