Sql - Chaining CTEs
📖 Read the lesson
Code Life
By Pierre Marsaa
Several CTEs
One
WITH
can define several CTEs, separated by commas. Badges and wins per trainer, side by side:
↗️
Loading editor...
↶
▶️ Run
↷
Each CTE has
one row per trainer
, so the joins cannot multiply anything — the same fan-out protection as derived tables, with names you can read.
Only one WITH
Write
WITH
once, then
name AS (…)
blocks separated by commas:
WITH a AS (…), b AS (…) SELECT …
. A second
WITH
is a syntax error.
Verify