Bird
0
0

You need to generate a full path string for each employee in a hierarchy stored in Employees(id, name, manager_id). Which recursive CTE approach is best suited for this?

hard📝 Application Q8 of 15
SQL - Common Table Expressions (CTEs)
You need to generate a full path string for each employee in a hierarchy stored in Employees(id, name, manager_id). Which recursive CTE approach is best suited for this?
ASelect only employees with no manager and ignore recursion
BConcatenate employee names in the recursive part to build the path from root to each employee
CUse a recursive CTE that counts levels but does not track names
DUse a non-recursive CTE with GROUP BY to aggregate names
Step-by-Step Solution
Solution:
  1. Step 1: Understand the requirement

    We want a full path string showing the chain of managers to each employee.
  2. Step 2: Recursive CTE approach

    Concatenate names in the recursive part to build the path progressively.
  3. Step 3: Why other options fail

    Ignoring recursion or only counting levels won't build the path string.
  4. Final Answer:

    Concatenate employee names in the recursive part to build the path from root to each employee -> Option B
  5. Quick Check:

    Path building requires string concatenation in recursion [OK]
Quick Trick: Build paths by concatenating names recursively [OK]
Common Mistakes:
  • Ignoring string concatenation in recursion
  • Using non-recursive queries for hierarchical paths
  • Only counting levels without path details

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes