Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

syntax

sql

oracle

People also ask

What is the difference between in and any in SQL?

Note that the <> ANY operator is different from NOT IN. 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.

What is any in Oracle?

The Oracle ANY operator is used to compare a value to a list of values or result set returned by a subquery. The following illustrates the syntax of the ANY operator when it is used with a list or subquery: operator ANY ( v1, v2, v3) operator ANY ( subquery)

What is the difference between in and exists in Oracle?

IN operator always picks the matching values list, whereas EXISTS returns the Boolean values TRUE or FALSE. EXISTS operator can only be used with subqueries, whereas we can use the IN operator on subqueries and values both.

Which is faster in or exists in Oracle?

Answers. Exist is more faster than IN because IN doesn't use indexes at the time of fetching but Exist uses Index at the time of fetching.


ANY (or its synonym SOME) is a syntax sugar for EXISTS with a simple correlation:

SELECT  *
FROM    mytable
WHERE   x <= ANY
        (
        SELECT  y
        FROM    othertable
        )

is the same as:

SELECT  *
FROM    mytable m
WHERE   EXISTS
        (
        SELECT  NULL
        FROM    othertable o
        WHERE   m.x <= o.y
        )

With the equality condition on a not-nullable field, it becomes similar to IN.

All major databases, including SQL Server, MySQL and PostgreSQL, support this keyword.


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


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.


I believe that what you are looking for is this:

http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96533/opt_ops.htm#1005298 (Link found on Eddie Awad's Blog) To sum it up here:

last_name IN ('SMITH', 'KING', 'JONES')

is transformed into

last_name = 'SMITH' OR last_name = 'KING' OR last_name = 'JONES'

while

salary > ANY (:first_sal, :second_sal)

is transformed into

salary > :first_sal OR salary > :second_sal

The optimizer transforms a condition that uses the ANY or SOME operator followed by a subquery into a condition containing the EXISTS operator and a correlated subquery


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).


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.