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

后端 未结 9 1349
难免孤独
难免孤独 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 04:08

    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.

    0 讨论(0)
  • 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

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

    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

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