Chained CTEs

Several CTEs can follow one another in a single WITH, separated by commas: each one can use those defined before it. This builds processing in named steps, from cleaning to final aggregation, without stacking nested subqueries.

Example

WITH a AS (...),
     b AS (SELECT * FROM a WHERE ...)
SELECT * FROM b;

Related concepts

The whole glossary