0
0
SQLquery~20 mins

Recursive CTE for hierarchical data in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Recursive CTE Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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;
A[{id: 1, name: 'Alice', level: 1}, {id: 2, name: 'Bob', level: 2}, {id: 3, name: 'Charlie', level: 2}, {id: 4, name: 'Diana', level: 3}]
B[{id: 1, name: 'Alice', level: 1}, {id: 2, name: 'Bob', level: 1}, {id: 3, name: 'Charlie', level: 1}, {id: 4, name: 'Diana', level: 1}]
C[{id: 4, name: 'Diana', level: 3}, {id: 3, name: 'Charlie', level: 2}, {id: 2, name: 'Bob', level: 2}, {id: 1, name: 'Alice', level: 1}]
D[{id: 1, name: 'Alice', level: 1}, {id: 3, name: 'Charlie', level: 2}, {id: 2, name: 'Bob', level: 2}, {id: 4, name: 'Diana', level: 3}]
Attempts:
2 left
💡 Hint
Think about how the recursion starts from the top-level manager and adds employees level by level.
📝 Syntax
intermediate
2: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;
AThe recursive part uses 'depth + 1' without table alias, causing ambiguity.
BThe base query is missing a WHERE clause to filter top-level managers.
CThe recursive CTE is missing the RECURSIVE keyword after WITH.
DThe JOIN condition uses 'e.manager_id = et.id' which is invalid.
Attempts:
2 left
💡 Hint
Check if all columns in the recursive part are properly qualified with table aliases.
optimization
advanced
3: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;
AAdd DISTINCT in the final SELECT: SELECT DISTINCT * FROM OrgChart;
BAdd a WHERE clause in the recursive part to exclude already visited ids.
CUse a CTE column to track visited ids and filter duplicates in the recursive step.
DReplace JOIN with LEFT JOIN in the recursive part to avoid duplicates.
Attempts:
2 left
💡 Hint
Think about how to track which rows have been processed to prevent revisiting them.
🧠 Conceptual
advanced
1:30remaining
Understanding Recursive CTE Termination Condition
What ensures that a recursive CTE for hierarchical data eventually stops and does not run infinitely?
AThe RECURSIVE keyword automatically limits recursion depth.
BThe base query selects rows with NULL manager_id, providing a starting point.
CThe recursive part must join on a condition that reduces the number of rows each iteration.
DThe recursion stops when no new rows are returned by the recursive SELECT.
Attempts:
2 left
💡 Hint
Consider what happens when the recursive SELECT returns zero rows.
🔧 Debug
expert
3: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;
AThe JOIN condition 'c.parent_id = ct.id' is incorrect and causes missing rows.
BThe WHERE clause 'c.is_active = TRUE' in the recursive part filters out some categories, reducing rows.
CThe base query should include 'c.is_active = TRUE' to match the recursive part.
DThe recursive CTE is missing the RECURSIVE keyword.
Attempts:
2 left
💡 Hint
Check filters in the recursive part that might exclude rows.