SQL JOIN - WHERE clause vs. ON clause

前端 未结 19 1587
深忆病人
深忆病人 2020-11-21 11:56

After reading it, this is not a duplicate of Explicit vs Implicit SQL Joins. The answer may be related (or even the same) but the question is diffe

19条回答
  •  孤独总比滥情好
    2020-11-21 12:54

    Are you trying to join data or filter data?

    For readability it makes the most sense to isolate these use cases to ON and WHERE respectively.

    • join data in ON
    • filter data in WHERE

    It can become very difficult to read a query where the JOIN condition and a filtering condition exist in the WHERE clause.

    Performance wise you should not see a difference, though different types of SQL sometimes handle query planning differently so it can be worth trying ¯\_(ツ)_/¯ (Do be aware of caching effecting the query speed)

    Also as others have noted, if you use an outer join you will get different results if you place the filter condition in the ON clause because it only effects one of the tables.

    I wrote a more in depth post about this here: https://dataschool.com/learn/difference-between-where-and-on-in-sql

提交回复
热议问题