2022-08-28 13:53:52 +00:00
---
2022-08-28 13:58:27 +00:00
slug: /en/sql-reference/operators/exists
2022-08-28 13:53:52 +00:00
---
2022-06-02 10:55:18 +00:00
# EXISTS
2021-11-12 20:02:26 +00:00
2021-11-15 19:49:22 +00:00
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` .
2021-11-12 20:02:26 +00:00
2023-11-30 09:57:44 +00:00
`EXISTS` can also be used in a [WHERE ](../../sql-reference/statements/select/where.md ) clause.
2021-11-13 20:00:00 +00:00
2023-03-27 18:54:05 +00:00
:::tip
2022-04-09 13:29:05 +00:00
References to main query tables and columns are not supported in a subquery.
:::
2021-11-12 20:02:26 +00:00
**Syntax**
2023-11-30 09:57:44 +00:00
``` sql
EXISTS(subquery)
2021-11-12 20:02:26 +00:00
```
**Example**
2023-11-30 09:57:44 +00:00
Query checking existence of values in a subquery:
``` sql
SELECT EXISTS(SELECT * FROM numbers(10) WHERE number > 8), EXISTS(SELECT * FROM numbers(10) WHERE number > 11)
```
Result:
``` text
┌─in(1, _subquery1)─┬─in(1, _subquery2)─┐
│ 1 │ 0 │
└───────────────────┴───────────────────┘
```
2021-11-18 19:44:38 +00:00
Query with a subquery returning several rows:
2021-11-12 20:02:26 +00:00
``` sql
2021-11-18 19:44:38 +00:00
SELECT count() FROM numbers(10) WHERE EXISTS(SELECT number FROM numbers(10) WHERE number > 8);
2021-11-12 20:02:26 +00:00
```
2021-11-18 19:44:38 +00:00
Result:
2021-11-12 20:02:26 +00:00
``` text
2021-11-18 19:44:38 +00:00
┌─count()─┐
│ 10 │
└─────────┘
```
Query with a subquery that returns an empty result:
``` sql
SELECT count() FROM numbers(10) WHERE EXISTS(SELECT number FROM numbers(10) WHERE number > 11);
```
Result:
``` text
┌─count()─┐
│ 0 │
└─────────┘
2021-11-12 20:02:26 +00:00
```