Using multiple ='s for IN?
Question by David542
What would be the difference between doing:
SELECT person FROM population WHERE id = 1 or id = 2 or id = 3
and –
SELECT person FROM population WHERE id IN (1,2,3)
Are they executed the exact same way? What difference is there? Would there ever be a reason where one would you IN
rather than multiple =
‘s?
Answer by Starx
No, they perform the same thing. The IN
minimizes the query string. That’s all. Such statements help in query optimization.
One difference in these two comparison operators would be that IN
uses a SET
of values to compare, unlike the “=” or “<>” which takes a single value.
According to the manual:
if expr is equal to any of the values in the IN list, else returns 0.
If all values are constants, they are evaluated according to the type
of expr and sorted. The search for the item then is done using a
binary search. This means IN is very quick if the IN value list
consists entirely of constants.