Skip to main content

WITH (CTE)

Common Table Expressions give a name to a subquery that can be referenced multiple times in the main query. CTE using SELECT:
CTE using VALUES:
Quoted CTE names and quoted CTE column lists are also supported:

Multiple CTEs

Define multiple CTEs in a single WITH block. Later CTEs may reference earlier ones:

Scalar Subquery in WHERE

A subquery that returns a single value can be used in a comparison:

Correlated Subquery in WHERE

The subquery references a column from the outer query:

Subquery in FROM (Derived Table)

Scalar Subquery in SELECT List

A subquery that returns a single value can appear as a column expression:
Correlated scalar subqueries in the SELECT list are also supported:

Subquery in HAVING

Scalar subqueries can be used in HAVING predicates:

Subquery in ORDER BY

Scalar subqueries can be used in ORDER BY expressions:

Not Supported

  • WITH RECURSIVE (recursive CTEs)