Hone logo
Hone
Problems

Hierarchical Data Navigation with Recursive CTEs

This challenge focuses on effectively querying hierarchical data structures using SQL's recursive Common Table Expressions (CTEs). You will simulate navigating through an organizational hierarchy, calculating levels and paths, which is a common task in database management for understanding relationships and reporting.

Problem Description

You are tasked with developing a SQL query to represent and traverse an employee hierarchy. Assume you have a table containing employees, where each employee has an employee_id, a name, and an manager_id. The manager_id is a foreign key referencing the employee_id of their direct manager. The top-level employee (e.g., CEO) will have a NULL manager_id.

Your goal is to write a recursive CTE that, given a starting employee (e.g., a specific manager), returns a list of all employees reporting to them, directly or indirectly. For each employee returned, you should also include their reporting level within the hierarchy (starting with 0 for the direct reports) and a comma-separated string representing the path from the starting employee to them.

Key Requirements:

  • Use a recursive CTE to traverse the hierarchy.
  • Identify direct and indirect reports of a given starting employee.
  • Calculate the reporting level for each employee. The direct reports of the starting employee are at level 0, their direct reports are at level 1, and so on.
  • Construct a path string for each employee, showing the sequence of employee IDs from the starting employee to the current employee, separated by ' -> '.
  • The query should accept a parameter to specify the start_employee_id.

Expected Behavior:

The CTE should start with the direct reports of the start_employee_id. In each recursive step, it should find employees whose manager is one of the employees identified in the previous step.

Edge Cases to Consider:

  • What happens if the start_employee_id does not exist in the employee table? (The query should return an empty set).
  • What if an employee has no direct reports? (They should not appear in the results unless they are the starting employee and have no subordinates).
  • Cycles in the hierarchy (e.g., employee A manages B, and B manages A). Recursive CTEs typically have built-in mechanisms to prevent infinite loops, but it's good to be aware of this possibility in real-world data. For this challenge, assume no cycles.

Examples

Example 1:

Input Employee Table (employees):

employee_idnamemanager_id
1AliceNULL
2Bob1
3Charlie1
4David2
5Eve2
6Frank3

Input start_employee_id: 1

Output:

employee_idnamemanager_idlevelpath
2Bob101 -> 2
3Charlie101 -> 3
4David211 -> 2 -> 4
5Eve211 -> 2 -> 5
6Frank311 -> 3 -> 6

Explanation: Alice (ID 1) is the starting point. Bob (2) and Charlie (3) directly report to Alice, so they are at level 0, and their paths start with '1 -> '. David (4) and Eve (5) report to Bob (2), so they are at level 1, and their paths extend from Bob's path. Frank (6) reports to Charlie (3), so he is at level 1, and his path extends from Charlie's path.

Example 2:

Input Employee Table (employees): (Same as Example 1)

Input start_employee_id: 2

Output:

employee_idnamemanager_idlevelpath
4David202 -> 4
5Eve202 -> 5

Explanation: Starting with Bob (ID 2). David (4) and Eve (5) directly report to Bob, so they are at level 0, and their paths start with '2 -> '.

Example 3: (Edge Case - No Reports)

Input Employee Table (employees): (Same as Example 1)

Input start_employee_id: 4

Output:

(Empty Set)

Explanation: David (ID 4) has no employees reporting to him, directly or indirectly.

Constraints

  • The employees table will contain at least 1 and at most 1000 rows.
  • employee_id will be a unique integer for each employee.
  • name will be a non-empty string.
  • manager_id will be an integer referencing another employee_id or NULL.
  • The recursion depth will not exceed 50 levels.
  • The maximum length of the path string will not exceed 500 characters.

Notes

  • A Common Table Expression (CTE) is a temporary named result set that you can reference within a single SQL statement.
  • A recursive CTE requires two parts: an anchor member (the base case) and a recursive member (the recursive step).
  • The anchor member typically selects the initial rows to start the recursion.
  • The recursive member references the CTE itself to build upon the previous results.
  • The UNION ALL or UNION operator is used to combine the results of the anchor and recursive members.
  • Ensure your query handles the NULL manager_id for the top-level employee correctly.
  • You can use pseudocode or your preferred SQL dialect for the solution, focusing on the logic of the recursive CTE.
Loading editor...
plaintext