subquery
A query written in brackets inside another query, whose result the outer query uses.
In more detail
A scalar subquery returns one value, a list subquery feeds IN, and a subquery in FROM acts like a temporary table. A correlated subquery refers to the outer row and is evaluated per row. Use a join instead when the related rows belong in the output.
Related terms
- scalar subquery — A subquery that returns exactly one value, one row and one column, so it can be used wherever a single value can.
- correlated subquery — A subquery that refers to a column of the outer query's current row, so its answer differs row by row.
- IN — An operator that is true when a value equals any value in a list or in the rows a subquery returns.
- EXISTS — An operator that is true when a subquery returns at least one row, whatever the row contains.
- common table expression — A named intermediate result, written in a WITH clause before the main query, that later parts of the query can use like a table.
Taught in
- Filtering With The WHERE Clause — PostgreSQL, Indexing & Relational Data Modeling
- Grouping By Multiple Columns — PostgreSQL, Indexing & Relational Data Modeling
- Subqueries — PostgreSQL, Indexing & Relational Data Modeling