Before continuing: This entry is part of the CTE guide. If you are new to the topic, start with WITH and CTE.
Example
The teaching database already contains employees.manager_id. Carmen has no manager; Alex, Carlos and Gonzalo report to Carmen; and further levels branch out from them.
WITH RECURSIVE organization AS (
SELECT id,
name,
manager_id,
0 AS level,
name AS route
FROM employees
WHERE manager_id IS NULL
UNION ALL
SELECT e.id,
e.name,
e.manager_id,
o.level + 1,
o.route || ' > ' || e.name
FROM employees AS e
JOIN organization AS o
ON e.manager_id = o.id
)
SELECT name, level, route
FROM organization
ORDER BY route;The starting case produces only Carmen. The recursive branch uses each row it has found to look for employees whose manager_id matches that id. level + 1 records the depth, and route shows the path that led to each person.
How it is used
Read the two branches separately. The first defines where the walk starts. The second says how to find rows related to the ones already in the CTE. UNION ALL combines both sets without removing duplicates automatically.
The step that moves forward is part of getting it right. A recursion with no real way to stop can repeat work, hit engine limits or walk the data incorrectly. With real hierarchical data, you also need to think about what would happen if there were a cycle.
What changes from case to case
UNION ALL is common because it does not force deduplication on every iteration. But termination does not come from mechanically swapping it for UNION: it depends on the recursive step following a relationship that eventually stops producing rows, and on the data not containing uncontrolled cycles.
In a well-formed hierarchy, a key like manager_id moves down towards descendants and reaches leaves: people with nobody reporting to them. In general graphs, preventing cycles needs an explicit strategy and can vary with the engine and the problem.
Avoid this confusion
Not separating the starting case from the recursive step
If you cannot explain which rows appear before the first repetition, the query is hard to verify. Work through the starting branch in your head first, then a single iteration of the recursive one.
Joining in the wrong direction
To go down an organisation chart, the child row must point to the id of the row already found: e.manager_id = o.id. Reversing that relationship reverses the direction of the walk.
Confusing the order of the walk with the order of the result
The recursion decides which rows are generated. The final presentation still needs an ORDER BY if you want a predictable order.
Key differences between engines
PostgreSQL and SQLite document recursive CTEs using WITH RECURSIVE. The rules for search order, cycle detection, recursion limits and optimisation are not identical across engines, so a production query must be checked on the specific engine.
AprenderSQL.es's built-in teaching engine teaches and runs ordinary CTEs, but it does not simulate recursive CTEs. The results on this page were checked with real SQLite on the same teaching data, not with the course lab.