Subqueries¶
A subquery is a query nested inside another query. Subqueries can appear in various parts of a SQL statement and can be correlated (referencing columns from outer queries) or non-correlated (independent of outer queries).
Subquery Types¶
EXISTS Subqueries¶
Tests whether a subquery returns any rows:
SELECT columns FROM table WHERE EXISTS (subquery)
Subqueries in FROM Clause¶
A subquery can appear in the FROM clause as a derived table:
SELECT columns FROM (subquery) AS alias
Array Unnesting¶
FDB supports PartiQL-style array unnesting:
SELECT columns FROM table, table.array_column AS alias
Examples¶
Setup¶
For these examples, assume we have the following tables:
CREATE TABLE a(ida INTEGER, x INTEGER, PRIMARY KEY(ida))
CREATE TABLE x(idx INTEGER, y INTEGER, PRIMARY KEY(idx))
CREATE TABLE b(idb INTEGER, q INTEGER, r INTEGER, PRIMARY KEY(idb))
CREATE TYPE AS STRUCT s(f INTEGER)
CREATE TABLE r(idr INTEGER, nr s ARRAY, PRIMARY KEY(idr))
INSERT INTO a VALUES (1, 1), (2, 2), (3, 3)
INSERT INTO x VALUES (4, 10), (5, 20), (6, 30)
INSERT INTO b VALUES (1, 10, 100), (2, 20, 200), (3, 30, 300)
INSERT INTO r VALUES
(1, [(11), (12), (13)]),
(2, [(21), (22), (23)]),
(3, [(31), (32), (33)])
Array Unnesting with PartiQL¶
Unnest an array column using PartiQL syntax:
SELECT idr FROM r, r.nr AS nest WHERE nest.f = 23
|
|---|
|
This query iterates over the nr array in each row of r and returns rows where the nested struct’s f field equals 23.
You can also use a subquery for array unnesting:
SELECT idr FROM r, (SELECT * FROM r.nr) AS nest WHERE nest.f = 23
|
|---|
|
Correlation in Aggregation Functions¶
Correlations can also appear inside aggregate functions:
SELECT x FROM a
WHERE EXISTS (
SELECT MAX(a.x), MAX(idb) FROM b
WHERE q > x
GROUP BY q
)
|
|---|
|
|
|
Here, MAX(a.x) aggregates the correlated column a.x, which has the same value for all rows processed by each instance of the subquery.
Important Notes¶
Subqueries in the FROM clause create a new query scope. Columns from the subquery are accessed via the subquery alias:
SELECT sq.column FROM (SELECT column FROM table) AS sq
When a column reference is not qualified with a table alias, the semantic analyzer first tries to resolve it in the current query block. If not found, it looks in the outer query blocks:
-- In the subquery, 'x' refers to column x from table a, not table x
SELECT idx FROM x WHERE EXISTS (SELECT x FROM a WHERE ida = 1)