Oracle: '= ANY()' vs. 'IN ()'

后端 未结 9 1347
难免孤独
难免孤独 2020-12-01 03:37

I just stumbled upon something in ORACLE SQL (not sure if it\'s in others), that I am curious about. I am asking here as a wiki, since it\'s hard to try to search symbols in

相关标签:
9条回答
  • 2020-12-01 03:49
    IN- Equal to any member in the list
    ANY- Compare value to **each** value returned by the subquery
    ALL- Compare value to **EVERY** value returned by the subquery
    
    <ANY() - less than maximum
    >ANY() - more than minimum
    =ANY() - equivalent to IN
    >ALL() - more than the maximum
    <ALL() - less than the minimum
    

    eg:

    Find the employees who earn the same salary as the minimum salary for each department:

    SELECT last_name, salary,department_id
    FROM employees
    WHERE salary IN (SELECT MIN(salary)
                     FROM employees
                     GROUP BY department_id);
    

    Employees who are not IT Programmers and whose salary is less than that of any IT programmer:

    SELECT employee_id, last_name, salary, job_id
    FROM employees
    WHERE salary <ANY
                    (SELECT salary
                     FROM employees
                     WHERE job_id = 'IT_PROG')
    AND job_id <> 'IT_PROG';
    

    Employees whose salary is less than the salary ofall employees with a job ID of IT_PROG and whose job is not IT_PROG:

    SELECT employee_id,last_name, salary,job_id
    FROM employees
    WHERE salary <ALL
                    (SELECT salary
                     FROM employees
                     WHERE job_id = 'IT_PROG')
    AND job_id <> 'IT_PROG';
    

    ....................

    Hope it helps. -Noorin Fatima

    0 讨论(0)
  • 2020-12-01 03:49

    A quick google found this http://theopensourcery.com/sqlanysomeall.htm

    Any allows you to use an operator other than = , in most other respect (special cases for nulls) it acts like IN. You can think of IN as ANY with the = operator.

    0 讨论(0)
  • 2020-12-01 04:02

    The ANY syntax allows you to write things like

    WHERE x > ANY(a, b, c)
    

    or event

    WHERE x > ANY(SELECT ... FROM ...)
    

    Not sure whether there actually is anyone on the planet who uses ANY (and its brother ALL).

    0 讨论(0)
  • 2020-12-01 04:03

    MySql clears up ANY in it's documentation pretty well:

    The ANY keyword, which must follow a comparison operator, means “return TRUE if the comparison is TRUE for ANY of the values in the column that the subquery returns.” For example:

    SELECT s1 FROM t1 WHERE s1 > ANY (SELECT s1 FROM t2);

    Suppose that there is a row in table t1 containing (10). The expression is TRUE if table t2 contains (21,14,7) because there is a value 7 in t2 that is less than 10. The expression is FALSE if table t2 contains (20,10), or if table t2 is empty. The expression is unknown (that is, NULL) if table t2 contains (NULL,NULL,NULL).

    https://dev.mysql.com/doc/refman/5.5/en/any-in-some-subqueries.html

    Also Learning SQL by Alan Beaulieu states the following:

    Although most people prefer to use IN, using = ANY is equivalent to using the IN operator.

    0 讨论(0)
  • 2020-12-01 04:05

    To put it simply and quoting from O'Reilly's "Mastering Oracle SQL":

    "Using IN with a subquery is functionally equivalent to using ANY, and returns TRUE if a match is found in the set returned by the subquery."

    "We think you will agree that IN is more intuitive than ANY, which is why IN is almost always used in such situations."

    Hope that clears up your question about ANY vs IN.

    0 讨论(0)
  • 2020-12-01 04:06

    Perhaps one of the linked articles points this out, but isn't it true that when looking for a match (=) the two return the same thing. However, if looking for a range of answers (>, <, etc) you cannot use "IN" and would have to use "ANY"...

    I'm a newb, forgive me if I've missed something obvious...

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