SQLSQL
#39

Traverse Organizational Hierarchy with Recursive CTEs in SQL

HardPwC
Interview Question #39
Asked at PwC

Given `employees` (emp_id, name, manager_id), write a recursive CTE to output each employee, their management depth level (CEO = 1, their direct reports = 2, etc.), and their full breadcrumb reporting path.

Input Table: employees
3 rows preview
emp_idnamemanager_id
1RajeshNULL
2Kaushal1
3Ankit2
Expected Output Structure3 rows
emp_idnamelevelpath
1Rajesh1Rajesh
2Kaushal2Rajesh -> Kaushal
3Ankit3Rajesh -> Kaushal -> Ankit
Interview Context

Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.

SQLPostgreSQL 15
Ready to execute

Click "Run Query" or press Ctrl + Enter to test your query.

Output will be verified against the test dataset.

Chat with us