correlated subquery
A subquery that refers to a column of the outer query's current row, so its answer differs row by row.
In more detail
WHERE EXISTS (SELECT 1 FROM orders AS o WHERE o.customer_id = c.id) asks, for each customer c, whether any order points at them. Logically it runs once per outer row, although the planner may execute it more cleverly.
Related terms
- subquery — A query written in brackets inside another query, whose result the outer query uses.
- EXISTS — An operator that is true when a subquery returns at least one row, whatever the row contains.
Taught in
- Subqueries — PostgreSQL, Indexing & Relational Data Modeling