Challenge - 5 Problems
Recursive CTE Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of a simple recursive CTE
Given the table
employees with columns id, name, and manager_id, what is the output of this query?WITH RECURSIVE emp_cte AS ( SELECT id, name, manager_id, 1 AS level FROM employees WHERE manager_id IS NULL UNION ALL SELECT e.id, e.name, e.manager_id, c.level + 1 FROM employees e JOIN emp_cte c ON e.manager_id = c.id ) SELECT id, name, level FROM emp_cte ORDER BY level, id;
PostgreSQL
WITH RECURSIVE emp_cte AS ( SELECT id, name, manager_id, 1 AS level FROM employees WHERE manager_id IS NULL UNION ALL SELECT e.id, e.name, e.manager_id, c.level + 1 FROM employees e JOIN emp_cte c ON e.manager_id = c.id ) SELECT id, name, level FROM emp_cte ORDER BY level, id;
Attempts:
2 left
💡 Hint
Think about how the recursion builds levels starting from the top manager.
✗ Incorrect
The recursive CTE starts with employees who have no manager (level 1). Then it finds employees managed by them, increasing the level by 1 each time. The output shows employees with their correct hierarchical level.
📝 Syntax
intermediate1:30remaining
Identify the syntax error in recursive CTE
Which option contains a syntax error in this recursive CTE query?
WITH RECURSIVE dept_tree AS ( SELECT id, name, parent_id FROM departments WHERE parent_id IS NULL UNION ALL SELECT d.id, d.name, d.parent_id FROM departments d JOIN dept_tree dt ON d.parent_id = dt.id ) SELECT * FROM dept_tree;
PostgreSQL
WITH RECURSIVE dept_tree AS ( SELECT id, name, parent_id FROM departments WHERE parent_id IS NULL UNION ALL SELECT d.id, d.name, d.parent_id FROM departments d JOIN dept_tree dt ON d.parent_id = dt.id ) SELECT * FROM dept_tree;
Attempts:
2 left
💡 Hint
Check if the recursive CTE follows the correct syntax for PostgreSQL.
✗ Incorrect
The query correctly defines a recursive CTE with a base case and a recursive part joined by UNION ALL. There is no syntax error.
❓ optimization
advanced2:30remaining
Optimize recursive CTE for large hierarchy
You have a large employee hierarchy and want to optimize this recursive CTE:
Which change will improve performance by preventing unnecessary recursion?
WITH RECURSIVE emp_path AS ( SELECT id, name, manager_id, ARRAY[id] AS path FROM employees WHERE manager_id IS NULL UNION ALL SELECT e.id, e.name, e.manager_id, path || e.id FROM employees e JOIN emp_path ep ON e.manager_id = ep.id WHERE NOT e.id = ANY(path) ) SELECT * FROM emp_path;
Which change will improve performance by preventing unnecessary recursion?
PostgreSQL
WITH RECURSIVE emp_path AS ( SELECT id, name, manager_id, ARRAY[id] AS path FROM employees WHERE manager_id IS NULL UNION ALL SELECT e.id, e.name, e.manager_id, path || e.id FROM employees e JOIN emp_path ep ON e.manager_id = ep.id WHERE NOT e.id = ANY(path) ) SELECT * FROM emp_path;
Attempts:
2 left
💡 Hint
Think about how the database finds rows matching manager_id in the join.
✗ Incorrect
Adding an index on the manager_id column speeds up the join operation in the recursive part, reducing query time. Removing the cycle check or changing UNION ALL would cause errors or inefficiencies.
🔧 Debug
advanced2:00remaining
Why does this recursive CTE cause infinite recursion?
Consider this recursive CTE:
It causes infinite recursion. What is the most likely reason?
WITH RECURSIVE cat_tree AS ( SELECT id, name, parent_id FROM categories WHERE parent_id IS NULL UNION ALL SELECT c.id, c.name, c.parent_id FROM categories c JOIN cat_tree ct ON c.parent_id = ct.id ) SELECT * FROM cat_tree;
It causes infinite recursion. What is the most likely reason?
PostgreSQL
WITH RECURSIVE cat_tree AS ( SELECT id, name, parent_id FROM categories WHERE parent_id IS NULL UNION ALL SELECT c.id, c.name, c.parent_id FROM categories c JOIN cat_tree ct ON c.parent_id = ct.id ) SELECT * FROM cat_tree;
Attempts:
2 left
💡 Hint
Think about what happens if a category points to itself or creates a loop.
✗ Incorrect
If the categories table has cycles (e.g., a category is its own ancestor), the recursive CTE keeps looping infinitely. To fix, you must detect and prevent cycles.
🧠 Conceptual
expert3:00remaining
Understanding recursive CTE output structure
You run this recursive CTE to get a hierarchy path:
What does the
WITH RECURSIVE org_path AS ( SELECT id, name, parent_id, name::text AS path FROM org_units WHERE parent_id IS NULL UNION ALL SELECT o.id, o.name, o.parent_id, op.path || ' > ' || o.name FROM org_units o JOIN org_path op ON o.parent_id = op.id ) SELECT id, path FROM org_path ORDER BY path;
What does the
path column represent in the output?PostgreSQL
WITH RECURSIVE org_path AS ( SELECT id, name, parent_id, name::text AS path FROM org_units WHERE parent_id IS NULL UNION ALL SELECT o.id, o.name, o.parent_id, op.path || ' > ' || o.name FROM org_units o JOIN org_path op ON o.parent_id = op.id ) SELECT id, path FROM org_path ORDER BY path;
Attempts:
2 left
💡 Hint
Look at how the path is built by concatenating names with ' > '.
✗ Incorrect
The path column builds a string showing the chain of org units from the top root down to each unit, separated by ' > '. This helps visualize the full hierarchy path.