What's the difference between NOT EXISTS vs. NOT IN vs. LEFT JOIN WHERE IS NULL?

前端 未结 5 854
独厮守ぢ
独厮守ぢ 2020-11-22 00:56

It seems to me that you can do the same thing in a SQL query using either NOT EXISTS, NOT IN, or LEFT JOIN WHERE IS NULL. For example:

SELECT a FROM table1          


        
5条回答
  •  失恋的感觉
    2020-11-22 01:08

    • NOT IN vs. NOT EXISTS vs. LEFT JOIN / IS NULL: SQL Server

    • NOT IN vs. NOT EXISTS vs. LEFT JOIN / IS NULL: PostgreSQL

    • NOT IN vs. NOT EXISTS vs. LEFT JOIN / IS NULL: Oracle

    • NOT IN vs. NOT EXISTS vs. LEFT JOIN / IS NULL: MySQL

    In a nutshell:

    NOT IN is a little bit different: it never matches if there is but a single NULL in the list.

    • In MySQL, NOT EXISTS is a little bit less efficient

    • In SQL Server, LEFT JOIN / IS NULL is less efficient

    • In PostgreSQL, NOT IN is less efficient

    • In Oracle, all three methods are the same.

提交回复
热议问题