Question

What is the difference between IN and ANY operators in SQL ?

Was it helpful?

Solution

SQL>
SQL> -- Use the ANY operator in a WHERE clause to compare a value with any of the values in a list.
SQL>

SQL> -- You must place an =, <>, <, >, <=, or >= operator before ANY.

SQL>
SQL> SELECT *
  2  FROM employee
  3  WHERE salary > ANY (2000, 3000, 4000);

For In Operator

SQL> -- Use the IN operator in a WHERE clause to compare a value with any of the values in a list.

but with the In you cannot use =, <>, <, >, <=, or >=

OTHER TIPS

IN->Equal to Any One in the List.

ANY->Compares Value to Each Value Returned by the Sub Query.

ALL->Compares Value To Every Value Returned by the Sub Query.

For Example:

IN: (Q):Display the Details of all the Employees Whose Salaries are Matching with Least Investments of Departments?

(A): SQL>Select Ename Sal Deptno from Emp Where Sal IN(Select Min(Sal) From Emp Group By Deptno);

ANY:

Meaans Less Than The Maximum Value in the List.

(Q):Get The Details of All Employees Who are Earning Less Than The Highest Earning Employee Controling Other Emp?

(A): SQL>Select Empno Ename Job Sl From Emp Where Sal

ANY:->Meaans More Than The Minimum Value in the List.

(Q):Get The Details Of All Emps Who are Earning more than the least paid of Department 10?

(A): SQL>Select Empno Ename Job Sal From Emp Where Sal>Any(Select Min(Sal) From Emp Where Deptno 10);

ANY:->It's Equivalent to In Operator.

Note: 'Some' is also used insted of ANY.

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.

While using all

SELECT empno, sal FROM emp WHERE sal > ALL (2000, 3000, 4000);

 EMPNO        SAL

  7839       5000

It will return result equivalent to query:

SELECT empno, sal FROM emp WHERE sal > 2000 AND sal > 3000 AND sal > 4000;

While using any

SELECT empno, sal FROM emp WHERE sal > ANY (2000, 3000, 4000);

 EMPNO        SAL

  7566       2975
  7698       2850
  7782       2450
  7788       3000
  7839       5000
  7902       3000

Returns a result same as

SELECT empno, sal FROM emp WHERE sal > 2000 OR sal > 3000 OR sal > 4000;

ANY and ALL OPERATOR IN SQL SERVER 2008R2.

Using the > comparison operator as an example, >ALL means greater than every value--in other words, greater than the maximum value. For example, >ALL (1, 2, 3) means greater than 3. >ANY means greater than at least one value, that is, greater than the minimum. So >ANY (1, 2, 3) means greater than 1.

Similarly, >ANY means that for a row to satisfy the condition specified in the outer query, the value in the column that introduces the subquery must be greater than at least one of the values in the list of values returned by the subquery.

With ANY, you need an operator:

WHERE X > ANY (SELECT Y FROM Z)

With IN, you can't. It's always testing for equality.

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.

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:

(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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top