WIPIVERSE All articles

Nested SQL

Nested SQL refers to the practice, within the Structured Query Language (SQL), of embedding one SQL query inside another. The term is most commonly encountered in the forms nested query and subquery, which are treated as synonyms in standard relational database documentation. A query written inside another query is called the inner query or inner select, while the statement that contains it is called the outer query or outer select.

Definition and terminology

A subquery is defined as a query nested inside a SELECT, INSERT, UPDATE, or DELETE statement, or inside another subquery. The SELECT statement of a subquery is enclosed in parentheses. The qualifier "nested" derives from the fact that the inner query is logically contained within the enclosing statement, and nesting can be repeated so that a subquery itself contains further subqueries.

The phrase "Nested SQL" is not a formally standardized name; the recognized technical terms are "nested query" and "subquery." Accordingly, sources that use "nested SQL" generally mean nested queries or subqueries in SQL.

Execution model

Conceptually, the innermost query is evaluated first, and its result is passed to the enclosing query. Documentation notes that, in some implementations, the subquery results can be treated as if substituted into the outer query, although this is not necessarily how a database engine actually processes the statement. In practice, query optimizers may rewrite subqueries as equivalent joins.

Common forms

Subqueries typically appear in the following syntactic patterns:

  • WHERE expression [NOT] IN (subquery)
  • WHERE expression comparison_operator [ANY | ALL] (subquery)
  • WHERE [NOT] EXISTS (subquery)

They may also be used in place of an expression, for example as a column expression returning a single value.

Types

Documentation distinguishes several kinds of nested queries:

  • Scalar subqueries, which return a single value and are introduced by an unmodified comparison operator such as =, <, or >.
  • Multi-row (list) subqueries, which return a list of values and are introduced with IN, NOT IN, or a comparison operator modified by ANY, SOME, or ALL.
  • Existence tests, introduced with EXISTS or NOT EXISTS, which return TRUE or FALSE rather than data.
  • Correlated subqueries (also called repeating subqueries), which depend on the outer query for their values and are consequently evaluated repeatedly, once for each candidate row of the outer query.

Nesting depth

Nesting can occur at multiple levels. In Microsoft SQL Server, for example, up to 32 levels of nesting are documented as possible, though the practical limit varies with available memory and the complexity of other expressions in the query.

Relationship to joins

Many statements that include subqueries can alternatively be formulated as joins. Documentation observes that there is usually no performance difference between a statement containing a subquery and a semantically equivalent version without one, while noting that in some existence-check cases a join may yield better performance. Conversely, not all subqueries can be expressed as joins, because joins are symmetric in ordering whereas subqueries are not.

Constraints

Commonly documented restrictions on subqueries include: a subquery introduced by an unmodified comparison operator must return a single value and therefore cannot use GROUP BY or HAVING; ORDER BY is generally permitted only when TOP is also specified; certain large-object data types such as text, ntext, and image cannot appear in a subquery select list; and a view created using a subquery cannot be updated.

Context

Nested queries are a long-established feature of SQL and are documented across major database platforms and educational references, including Microsoft's SQL Server documentation, general SQL tutorials, and database textbooks. The concept is therefore well recognized under the names "nested query" and "subquery," even though "Nested SQL" itself is a descriptive rather than a formal term.

Browse

More topics to explore

    Browse all articles