Skip to main content

EXISTS

The EXISTS operator checks how many records are in the result of a subquery. If it is empty, then the operator returns 0. Otherwise, it returns 1.

EXISTS can be used in a WHERE clause.

warning

References to main query tables and columns are not supported in a subquery.

Syntax

WHERE EXISTS(subquery)

Example

Query with a subquery returning several rows:

SELECT count() FROM numbers(10) WHERE EXISTS(SELECT number FROM numbers(10) WHERE number > 8);

Result:

โ”Œโ”€count()โ”€โ”
โ”‚ 10 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Query with a subquery that returns an empty result:

SELECT count() FROM numbers(10) WHERE EXISTS(SELECT number FROM numbers(10) WHERE number > 11);

Result:

โ”Œโ”€count()โ”€โ”
โ”‚ 0 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜