Subqueries

Nest queries within other SQL statements. Learn to use scalar, column, row, and table subqueries to filter or calculate data dynamically.

Compare a value against all results from a subquery. The ALL operator returns TRUE if the comparison holds for every row returned by the subquery.

Compare a value against any result from a subquery. The ANY (or SOME) operator returns TRUE if the comparison holds for at least one row. IN can be used for =ANY.

Test for the existence of rows. The EXISTS operator returns TRUE if the subquery returns at least one row, often used for correlated subqueries.

Understand when to use subqueries versus joins. This guide explains performance implications and how to rewrite subqueries as joins for efficiency.

Use a subquery as a temporary table. Derived tables allow you to select from the result set of another query within the FROM clause.

Retrieve a single row of multiple values. A row subquery returns a tuple that can be compared against a row constructor in the outer query.

Return a single value. A scalar subquery produces a one-row, one-column result that can be used anywhere a constant or expression is valid.

Review restrictions on subquery usage. This page details unsupported operations, such as modifying a table while selecting from it in a subquery.

Last updated

Was this helpful?