Difference between EXISTS and IN in SQL?

前端 未结 21 1733
执笔经年
执笔经年 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:32

    The reason is that the EXISTS operator works based on the “at least found” principle. It returns true and stops scanning table once at least one matching row found.

    On the other hands, when the IN operator is combined with a subquery, MySQL must process the subquery first, and then uses the result of the subquery to process the whole query.

    The general rule of thumb is that if the subquery contains a large volume of data, the EXISTS operator provides a better performance.

    However, the query that uses the IN operator will perform faster if the result set returned from the subquery is very small.

提交回复
热议问题