Problem Statement
What are the two required parts of a recursive CTE?
Explanation
The anchor produces the seed rows. The recursive member references the CTE and expands the result, usually via UNION ALL. A safety condition limits depth.
This pattern models hierarchies like org charts and folder trees without procedural code.
Code Solution
SolutionRead Only
WITH RECURSIVE org AS ( SELECT id, manager_id, 1 AS lvl FROM employees WHERE manager_id IS NULL UNION ALL SELECT e.id, e.manager_id, o.lvl+1 FROM employees e JOIN org o ON e.manager_id = o.id ) SELECT * FROM org;
