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

后端 未结 5 905
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:23

    SELECT Customers.* 
      FROM Customers 
     WHERE NOT EXISTS (
           SELECT *
             FROM SUBSCRIBERS AS s
             JOIN s.Cust_ID = Customers.Customer_ID) 
    

    When using “NOT IN”, the query performs nested full table scans, whereas for “NOT EXISTS”, the query can use an index within the sub-query.

提交回复
热议问题