Bird
0
0

How does a recursive CTE help in retrieving all levels of subordinates for a manager in an employee hierarchy table employees(id, name, manager_id)?

hard📝 Application Q9 of 15
SQL - Common Table Expressions (CTEs)
How does a recursive CTE help in retrieving all levels of subordinates for a manager in an employee hierarchy table employees(id, name, manager_id)?
ABy repeatedly joining the CTE to itself to traverse the hierarchy until no more subordinates are found
BBy using a simple GROUP BY to aggregate all subordinates under a manager
CBy creating temporary tables for each level of subordinates manually
DBy filtering employees with a WHERE clause on manager_id only once
Step-by-Step Solution
Solution:
  1. Step 1: Understand recursive CTEs

    Recursive CTEs allow a query to refer to its own output to perform iterative processing.
  2. Step 2: Apply to employee hierarchy

    The recursive CTE starts with direct subordinates and repeatedly joins to find indirect subordinates.
  3. Final Answer:

    By repeatedly joining the CTE to itself to traverse the hierarchy until no more subordinates are found -> Option A
  4. Quick Check:

    Recursive self-join is key for hierarchical queries [OK]
Quick Trick: Recursive CTEs traverse hierarchies iteratively [OK]
Common Mistakes:
  • Using GROUP BY instead of recursion
  • Manually creating multiple temporary tables
  • Filtering only direct subordinates

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes