← 返回 tesla 的题目列表Find All Reports Under a Manager (n-level) and Return Hierarchy Path (SQL)
类型:online_judge
You have an organization table organization(employee_id, manager_id, name).
Find all employees who report to manager_id = 1, including both direct and indirect reports (n levels deep).
Requirements:
Return the full reporting hierarchy path.
Output: employee_id, manager_id, name, level, hierarchy_path.
level: depth relative to manager 1 (direct reports = 1).
hierarchy_path: path from manager 1 to the employee (define the exact format, e.g., 1->3->8 or names).
Note:
Assume the data should form a tree/forest; explain how you would prevent or handle cycles.
Example
Input
employee_id,manager_id,name
1,,CEO
2,1,A
3,1,B
4,2,C
5,4,D
Output
2,1,A,1,1->2
3,1,B,1,1->3
4,2,C,2,1->2->4
5,4,D,3,1->2->4->5