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
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.
This is a standard. The SQL 1992 standard states
8.4 <in predicate>
[...]
<in predicate> ::= <row value constructor> [ NOT ] IN <in predicate value>
[...]
2) Let RVC be the <row value constructor> and let IPV be the <in predicate value>.
[...]
4) The expression
RVC IN IPV
is equivalent to
RVC = ANY IPV
So in fact, the <in predicate>
behaviour definition is based on the 8.7 <quantified comparison predicate>
. In Other words, Oracle correctly implements the SQL standard here
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