EXISTS is a boolean operator used in WHERE, HAVING, JOIN ON, or CASE expressions to test whether a subquery yields any rows. The database engine stops scanning the subquery once it finds the first qualifying row, making EXISTS highly efficient for existence checks. EXISTS does not return the rows themselves; it only evaluates to TRUE or FALSE for each row in the outer query. Because EXISTS ignores the columns listed in the SELECT clause of its subquery, many developers use SELECT 1 or SELECT NULL for clarity, but any valid select list is accepted. A correlated subquery can reference columns from the outer query, allowing row-by-row checks. NOT EXISTS inverts the result, returning TRUE when the subquery is empty. Caveats: ORDER BY is disallowed unless combined with LIMIT/OFFSET; DISTINCT inside the subquery rarely improves performance; avoid returning large result sets in the subquery because some optimizers may materialize them when correlation is absent.
subquery
(required) - A SELECT statement enclosed in parentheses evaluated for each row of the outer query.NOT EXISTS, IN, ANY/SOME, ALL, JOIN, WHERE, HAVING, Subqueries, Correlated Subquery
SQL-86 (adopted by all major databases)
EXISTS returns TRUE when the subquery has rows. NOT EXISTS returns TRUE when the subquery is empty, letting you express anti joins.
For existence checks, EXISTS often outperforms JOIN because the database can stop at the first match and avoid materializing result sets.
Standard SQL forbids ORDER BY unless it is paired with LIMIT or OFFSET. Ordering is irrelevant because the engine stops after the first row.
No. Any valid select list works, but SELECT 1 or SELECT NULL clarifies that the data is unused.