Difference between EXISTS and IN in SQL?

前端 未结 21 1799
执笔经年
执笔经年 2020-11-22 16:50

What is the difference between the EXISTS and IN clause in SQL?

When should we use EXISTS, and when should we use IN

21条回答
  •  佛祖请我去吃肉
    2020-11-22 17:18

    I think,

    • EXISTS is when you need to match the results of query with another subquery. Query#1 results need to be retrieved where SubQuery results match. Kind of a Join.. E.g. select customers table#1 who have placed orders table#2 too

    • IN is to retrieve if the value of a specific column lies IN a list (1,2,3,4,5) E.g. Select customers who lie in the following zipcodes i.e. zip_code values lies in (....) list.

    When to use one over the other... when you feel it reads appropriately (Communicates intent better).

提交回复
热议问题