Skip to content

Commit

Permalink
Merge pull request #8373 from soyeric128/scalar-subqueries
Browse files Browse the repository at this point in the history
docs: scalar subqueries
  • Loading branch information
mergify[bot] authored Oct 21, 2022
2 parents 8970988 + a384a3b commit 3c35a93
Showing 1 changed file with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,47 @@ description:
A subquery is a query nested within another query.
---

This topic provides reference information about the subquery operators supported in Databend.
A subquery is a query nested within another one. Databend supports the following subquery types:

A subquery is a query nested within another query.
- [Scalar Subquery](#scalar-subquery)
- [EXISTS / NOT EXISTS](#exists--not-exists)

## [ NOT ] EXISTS
## Scalar Subqueries

A scalar subquery selects only one column or expression and returns only one row at most. A SQL query can have scalar subqueries in any places where a column or expression is expected.

- If a scalar subquery returns 0 rows, Databend will use NULL as the subquery output.
- If a scalar subquery returns more than one row, Databend will throw an error.

### Example

```sql
CREATE TABLE t1 (a int);
CREATE TABLE t2 (a int);

INSERT INTO t1 VALUES (1);
INSERT INTO t1 VALUES (2);
INSERT INTO t1 VALUES (3);

INSERT INTO t2 VALUES (3);
INSERT INTO t2 VALUES (4);
INSERT INTO t2 VALUES (5);

SELECT *
FROM t1
WHERE t1.a < (SELECT Min(t2.a)
FROM t2);

--
+--------+
| a |
+--------+
| 1 |
| 2 |
+--------+
```

## EXISTS / NOT EXISTS

An EXISTS subquery is a boolean expression that can appear in a WHERE clause:
* An EXISTS expression evaluates to TRUE if any rows are produced by the subquery.
Expand Down

1 comment on commit 3c35a93

@vercel
Copy link

@vercel vercel bot commented on 3c35a93 Oct 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

databend – ./

databend-databend.vercel.app
databend.vercel.app
databend-git-main-databend.vercel.app
databend.rs

Please sign in to comment.