Recursive CTE (WITH RECURSIVE) Playground

The village staff chart: WITH RECURSIVE down and up, each round rerun by a real SQLite; a cycle in the import, a guard, UNION vs UNION ALL. An interactive…

Why it matters

Hierarchies are everywhere: org charts, nested categories, comment threads, routes. A recursive CTE walks one in a single query. Three things often go wrong: the recursive part sees only the previous round’s rows, cyclic data makes it run forever, and UNION drops only rows that are exactly the same.

The key idea

The anchor once, then the same step repeated over the last round’s rows until nothing new comes

Common mistakes

Try it in this playground