Problem Statement
In a recursive CTE, what are the two essential parts?
Explanation
The anchor member seeds the first set of rows. The recursive member references the CTE and expands the set until no new rows appear.
Always add a termination guard such as a depth limit to avoid infinite recursion.
Code Solution
SolutionRead Only
WITH RECURSIVE org AS ( SELECT id, manager_id, 1 AS lvl FROM emp WHERE manager_id IS NULL UNION ALL SELECT e.id, e.manager_id, o.lvl+1 FROM emp e JOIN org o ON e.manager_id=o.id ) SELECT * FROM org;
Practice Sets
This question appears in the following practice sets:
