Keywords: Subqueries, elimination of DISTINCT in IN, ANY, and EXISTS subqueries

# DISTINCT elimination in IN, ANY, and EXISTS subqueries

An IN, ANY, or EXISTS subquery evaluates to true if there is at least one row that causes the subquery to evaluate to true. These semantics make a DISTINCT within an IN, ANY, or EXISTS subquery unnecessary. The following two queries are equivalent and the first is transformed into the second:

    SELECT * FROM t1 WHERE c1 IN
        (SELECT DISTINCT c2 FROM t2 WHERE t1.c3 = t2.c4)

    SELECT * FROM t1 WHERE c1 IN
        (SELECT c2 FROM t2 WHERE t1.c3 = t2.c4)

Parent topic: Subquery processing and transformations
Related concepts
Materialization
Flattening a subquery into a normal join
Flattening a subquery into an EXISTS join
Flattening VALUES subqueries
IN/ANY subquery transformation