Skip to main content
Module 14 · Lesson 2 of 354/57 in the course~16 min
Module lessons (2/3)

Multiple and Chained CTEs

Lesson preview5 hands-on exercises readySQL Course · Module 14 — CTEs and Hierarchical Queries (WITH)

Build readable multi-stage analytical pipelines inside a single WITH clause.

What you'll learn in this lesson

  • Concatenate multiple sequential CTEs separated by comma (,) under a single keyword WITH.
  • Build readable multi-stage pipelines where each CTE transforms and aggregates the output of the previous CTE.
  • Combine one-row scalar CTEs with the main query via either subquery (SELECT ... FROM cte) or via CROSS JOIN.

Overview

A single CTE breaks the first level of nesting; But when building complex reports, segmented revenue, or comparisons against global metrics, the true power emerges when you chain two, three, or more CTEs into the same WITH block — one for each conceptual step of the data transformation.

Observe in the stepper how the CTE phase sequentially executes the intermediate steps transforming the data before even passing control to the external SELECT:

Sections covered in this lesson

  1. 11. Mental Model: Composable Stages Separated by Commas
  2. 22. Progressive & Annotated Examples
  3. 33. Interactive Laboratory: Multi-Stage Pipeline in Action
  4. 44. Diagnostic Verification (Quiz)
  5. 55. Progressive Practical Laboratory
Pro Lesson · Module 14 — CTEs and Hierarchical Queries (WITH)

Unlock "Multiple and Chained CTEs" and the full SQL Course curriculum

Build readable multi-stage analytical pipelines inside a single WITH clause.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in SQL Course plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.