SQL: … WHERE X IN (SELECT Y FROM …)

后端 未结 5 892
Happy的楠姐
Happy的楠姐 2021-02-12 11:30

Is the following the most efficient in SQL to achieve its result:

SELECT * 
  FROM Customers 
 WHERE Customer_ID NOT IN (SELECT Cust_ID FROM SUBSCRIBERS)
         


        
5条回答
  •  情书的邮戳
    2021-02-12 12:01

    One reason why you might prefer to use a JOIN rather than NOT IN is that if the Values in the NOT IN clause contain any NULLs you will always get back no results. If you do use NOT IN remember to always consider whether the sub query might bring back a NULL value!

    RE: Question in Comments

    'x' NOT IN (NULL,'a','b')

    ≡ 'x' <> NULL and 'x' <> 'a' and 'x' <> 'b'

    ≡ Unknown and True and True

    ≡ Unknown

提交回复
热议问题