From 398e164e46df4b0a79ec62c4e9445b646c22f95a Mon Sep 17 00:00:00 2001 From: Alexey Date: Fri, 12 Nov 2021 20:02:26 +0000 Subject: [PATCH] en draft (EXISTS) --- docs/en/sql-reference/operators/exists.md | 29 +++++++++++++++++++ docs/en/sql-reference/operators/index.md | 2 +- .../sql-reference/statements/select/where.md | 4 +++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 docs/en/sql-reference/operators/exists.md diff --git a/docs/en/sql-reference/operators/exists.md b/docs/en/sql-reference/operators/exists.md new file mode 100644 index 00000000000..929da871655 --- /dev/null +++ b/docs/en/sql-reference/operators/exists.md @@ -0,0 +1,29 @@ +# EXISTS {#exists-operator} + +The `EXISTS` operator tests how many records are in the result of a subquery. If it is empty, then the operator returns `0`. Otherwise it returns `1`. + +!!! warning "Warning" + References to main query tables and columns are not supported in a subquery. + +**Syntax** + +```sql +WHERE EXISTS(subquery) +``` + +**Example** + +Query: + +``` sql +SELECT 'Exists' WHERE EXISTS (SELECT * FROM numbers(10) WHERE number < 2); +SELECT 'Empty subquery' WHERE EXISTS (SELECT * FROM numbers(10) WHERE number > 12); +``` + +The first query returns one row. The second query does not return rows because the result of the subquery is empty. + +``` text +┌─'Exists'─┐ +│ Exists │ +└──────────┘ +``` diff --git a/docs/en/sql-reference/operators/index.md b/docs/en/sql-reference/operators/index.md index efbc600fdd4..1c59b25fc63 100644 --- a/docs/en/sql-reference/operators/index.md +++ b/docs/en/sql-reference/operators/index.md @@ -71,7 +71,7 @@ For tuple subtraction: [tupleMinus](../../sql-reference/functions/tuple-function ## Operators for Working with Data Sets {#operators-for-working-with-data-sets} -*See [IN operators](../../sql-reference/operators/in.md).* +See [IN operators](../../sql-reference/operators/in.md) and [EXISTS](../../sql-reference/operators/exists.md) operator. `a IN ...` – The `in(a, b)` function. diff --git a/docs/en/sql-reference/statements/select/where.md b/docs/en/sql-reference/statements/select/where.md index 69505a51db4..ffc9a38b5fb 100644 --- a/docs/en/sql-reference/statements/select/where.md +++ b/docs/en/sql-reference/statements/select/where.md @@ -12,3 +12,7 @@ If there is a `WHERE` clause, it must contain an expression with the `UInt8` typ !!! note "Note" There’s a filtering optimization called [prewhere](../../../sql-reference/statements/select/prewhere.md). + +**See Also** + +- [EXISTS](../../../sql-reference/operators/exists.md) operator