Recursive CTEs in SQL
Walk hierarchies and graphs with a query that calls itself
WITH RECURSIVE lets a CTE reference itself, turning SQL into a loop. An anchor query seeds the result, the recursive query repeatedly extends it — each pass seeing the previous pass’s rows — until nothing new appears. Org-chart reporting chains, bill-of-materials explosions and graph reachability all collapse from application loops into one declarative query.
Anchor + recursive half
WITH RECURSIVE t AS ( anchor UNION ALL recursive ) — the anchor seeds rows; the recursive half references t itself.
The anchor runs once
Its rows form the first working set and land in the output immediately.
Each pass extends the set
The recursive SELECT reads the previous pass’s rows only, joins or transforms them, and queues its output as the next working set.
Iteration ends when a pass is empty
No new rows → the loop terminates; the result is the UNION (or UNION ALL) of every pass.
Guard against cycles
Carry a depth/visited column and cap or filter it — a cyclic graph (an employee as their own manager) would otherwise loop forever.
1WITH RECURSIVE countdown (n) AS (2 SELECT 53 UNION ALL4 SELECT n - 1 FROM countdown WHERE n > 15)6SELECT n FROM countdown;
| n |
|---|
| 5 |
| 4 |
| 3 |
| 2 |
| 1 |
The anchor seeds n = 5. Pass 1 emits 4, pass 2 emits 3… each pass reading only the rows the previous pass produced. When n reaches 1 the WHERE blocks further rows, the loop ends, and the union of all passes is the countdown. Same mechanics, real data: employee → manager chains.
Countdown — the mechanics, bare
A minimal recursive CTE you can trace by hand.
1WITH RECURSIVE countdown (n) AS (2 SELECT 53 UNION ALL4 SELECT n - 1 FROM countdown WHERE n > 15)6SELECT n FROM countdown;
| n |
|---|
| 5 |
| 4 |
| 3 |
| 2 |
| 1 |
Management chain of one employee
The hierarchy use-case: walk up the org chart.
1WITH RECURSIVE chain AS (2 SELECT id, name, manager_id, 0 AS depth, name AS path3 FROM employees_x WHERE id = 64 UNION ALL5 SELECT m.id, m.name, m.manager_id, c.depth + 1, c.path || ' → ' || m.name6 FROM employees_x m JOIN chain c ON m.id = c.manager_id7)8SELECT path, depth FROM chain ORDER BY depth;
| path | depth |
|---|---|
| Farid | 0 |
| Cynthia → Farid | 1 |
| Cynthia | 2 |
Whole subtree below a node
Downward traversal — everyone under a manager.
1WITH RECURSIVE subtree AS (2 SELECT id, name FROM employees_x WHERE manager_id = 33 UNION ALL4 SELECT e.id, e.name5 FROM employees_x e JOIN subtree s ON e.manager_id = s.id6)7SELECT count(*) AS reports FROM subtree;
| reports |
|---|
| 3 |
Cycle guard with a depth cap
Bad data must not become an infinite loop.
1WITH RECURSIVE walk AS (2 SELECT id, parent_id, 1 AS depth FROM nodes WHERE id = 13 UNION ALL4 SELECT n.id, n.parent_id, w.depth + 15 FROM nodes n JOIN walk w ON n.parent_id = w.id6 WHERE w.depth < 507)8SELECT * FROM walk;9-- depth < 50 caps pathological data
| guarantee |
|---|
| recursion bounded at depth 50 — terminates even with a parent loop |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.