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 Recursive CTE for Employee Hierarchy
Given the following table Employees with columns
id, name, and manager_id, what is the output of this recursive CTE query?WITH RECURSIVE EmployeeHierarchy 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, eh.level + 1 FROM Employees e JOIN EmployeeHierarchy eh ON e.manager_id = eh.id ) SELECT id, name, level FROM EmployeeHierarchy ORDER BY level, id;
SQL
WITH RECURSIVE EmployeeHierarchy 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, eh.level + 1 FROM Employees e JOIN EmployeeHierarchy eh ON e.manager_id = eh.id ) SELECT id, name, level FROM EmployeeHierarchy ORDER BY level, id;
Attempts:
2 left
💡 Hint
Think about how the recursion starts from the top-level manager and adds employees level by level.
✗ Incorrect
The recursive CTE starts with employees who have no manager (level 1). Then it adds employees whose manager is in the previous level, increasing the level by 1 each time. The output is ordered by level and id, so Alice (id 1) is level 1, Bob and Charlie (ids 2 and 3) are level 2, and Diana (id 4) is level 3.
📝 Syntax
intermediate2:00remaining
Identify the Syntax Error in Recursive CTE
Which option contains a syntax error in the recursive CTE definition for hierarchical data?
SQL
WITH RECURSIVE EmployeeTree AS ( SELECT id, name, manager_id, 1 AS depth FROM Employees WHERE manager_id IS NULL UNION ALL SELECT e.id, e.name, e.manager_id, depth + 1 FROM Employees e JOIN EmployeeTree et ON e.manager_id = et.id ) SELECT * FROM EmployeeTree;
Attempts:
2 left
💡 Hint
Check if all columns in the recursive part are properly qualified with table aliases.
✗ Incorrect
In the recursive SELECT, 'depth + 1' is ambiguous because 'depth' is not prefixed with the alias 'et'. It should be 'et.depth + 1' to refer to the previous level's depth.
❓ optimization
advanced3:00remaining
Optimize Recursive CTE to Avoid Duplicate Rows
You have a recursive CTE that sometimes returns duplicate rows in the hierarchy. Which option best optimizes the query to avoid duplicates?
SQL
WITH RECURSIVE OrgChart 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, oc.level + 1 FROM Employees e JOIN OrgChart oc ON e.manager_id = oc.id ) SELECT * FROM OrgChart;
Attempts:
2 left
💡 Hint
Think about how to track which rows have been processed to prevent revisiting them.
✗ Incorrect
Tracking visited ids in a column (like a path or set) and filtering them in the recursive step prevents duplicates by ensuring each node is processed once.
🧠 Conceptual
advanced1:30remaining
Understanding Recursive CTE Termination Condition
What ensures that a recursive CTE for hierarchical data eventually stops and does not run infinitely?
Attempts:
2 left
💡 Hint
Consider what happens when the recursive SELECT returns zero rows.
✗ Incorrect
A recursive CTE stops when the recursive SELECT returns no new rows to add. This natural termination prevents infinite loops.
🔧 Debug
expert3:00remaining
Debugging Unexpected Output in Recursive CTE
A recursive CTE for a category tree returns fewer rows than expected. Which option is the most likely cause?
SQL
WITH RECURSIVE CategoryTree 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 CategoryTree ct ON c.parent_id = ct.id WHERE c.is_active = TRUE ) SELECT * FROM CategoryTree;
Attempts:
2 left
💡 Hint
Check filters in the recursive part that might exclude rows.
✗ Incorrect
Filtering with 'c.is_active = TRUE' in the recursive part excludes inactive categories and their descendants, causing fewer rows than expected.