Difference between IN and ANY operators in SQL

后端 未结 10 1765
無奈伤痛
無奈伤痛 2020-12-23 11:14

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

相关标签:
10条回答
  • 2020-12-23 11:52

    IN - Equals to Anything 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:

    Display the Details of all the Employees Whose Salaries are Matching with the Least Investments of Departments?

     Select Ename, Sal, Deptno 
     from Emp 
     Where Sal IN (Select Min(Sal) 
                   From Emp 
                   Group By Deptno);
    

    ANY:

    < ANY Means Less Than The Maximum Value in the List.

    Get The Details of All Employees Who are Earning Less Than The Highest Earning Employee Controlling Other Emp?

     Select Empno, Ename, Job, Sal 
     From Emp
     Where Sal < Any (Select Distinct MGR 
                      From Emp);
    

    > ANY Means More Than The Minimum Value in the List.

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

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

    =ANY is Equivalent to In Operator.

    Note: SOME is also used instead of ANY.

    0 讨论(0)
  • 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> 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.
    SQL> SELECT *
      2  FROM employee
      3  WHERE salary IN (2000, 3000, 4000);
    

    But with the IN operator you cannot use =, <>, <, >, <=, or >=

    0 讨论(0)
  • 2020-12-23 11:57

    IN - It is easy to understand. The query should select only those values which are specified in 'IN' clause. Now, let us understand 'ANY' with a query. ANY means it should be greater or less than any of the values in the list.

    Assume a Orders table which has OrderID from 1 to 10

    Observer the below query:
    select OrderID from Orders
    where OrderID < ANY (3,5,7)

    The answer to above query is :
    OrderID
    1,2,3,4,5,6

    Explanation :The query says find OrderIDs which are less than ANY of the specified values. So the database searches and includes OrderID as follows:
    Is 1<3- Yes hence OrderID 1 is included
    Is 2<3- Yes hence OrderID 2 is included
    Is 3<3- No, is 3<5 -Yes (as 5 is specified value), hence OrderID 3 is included
    Is 4<3- No, is 4<5 -Yes, hence OrderID 4 is included
    Is 5<3- No, is 5<5 -No, is 5<7(as 5 is specified value)-Yes hence OrderID 5 is included
    Is 6<3- No, is 6<5 -No, is 6<7-Yes hence OrderID 6 is included
    Is 7<3- No, is 7<5 -No, is 7<7-No hence OrderID 7 is NOT included as no more values in specified list to compare
    Is 8<3- No, is 8<5 -No, is 8<7-No hence OrderID 8 is NOT included as no more values in specified list to compare
    Is 9<3- No, is 9<5 -No, is 9<7-No hence OrderID 9 is NOT included as no more values in specified list to compare
    Is 9<3- No, is 9<5 -No, is 9<7-No hence OrderID 9 is NOT included as no more values in specified list to compare


    Apply the same logic for greater than
    select OrderID from Orders
    where OrderID > ANY (3,5,7)

    The answer to above query is :
    OrderID
    4,5,6,7,8,9,10

    0 讨论(0)
  • 2020-12-23 11:58

    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;

    0 讨论(0)
  • 2020-12-23 11:58

    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.

    0 讨论(0)
  • 2020-12-23 12:02

    With ANY, you need an operator:

    WHERE X > ANY (SELECT Y FROM Z)
    

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

    0 讨论(0)
提交回复
热议问题