What is the difference between IN
and ANY
operators in SQL?
The ANY and ALL operators are used with a WHERE or HAVING clause.
The ANY operator returns true if any of the subquery values meet the condition.
The ALL operator returns true if all of the subquery values meet the condition.
Maybe for better understanding, these two conditions are equivalent. It's a matter of taste which one you use (provided the RDBMS supports both of them)
... WHERE x IN (SELECT Y FROM THE_TABLE)
... WHERE x =ANY (SELECT Y FROM THE_TABLE)
and these also
... WHERE x NOT IN (SELECT Y FROM THE_TABLE)
... WHERE x <>ALL (SELECT Y FROM THE_TABLE)
Actually my personal habit is to use IN
for list expression (like WHERE x IN (2,4,6,8)
and =ANY
, resp. <>ALL
for sub-queries.
(in) is a special kind of operator which is use to pick value one by one from list of values which we have specified.while (any) is use with where clause
When we are comparing any column value using "IN" some set say {value1,value2 ...}
then the column value must be present in the set but in case of ANY we compare like this:
col.value > ANY ({value1,value2,...})
then the value must be greater than any one of the set value.
in case of "ALL"
col.value> ALL({value1,value2,...})
the value must be greater than all the values in the set.
Refer to the following images for better understanding: