Recursive Hierarchical Queries in SQL: A Deep Dive into Employee Levels

Remis Haroon
DataDrivenInvestor
Published in
3 min readSep 17, 2023

--

Hierarchy

In today’s data-driven world, hierarchical data structures are ubiquitous. From organizational charts to file systems, understanding and navigating these hierarchies is crucial. In this article, we’ll explore a common scenario involving employee hierarchies and demonstrate how to use recursive queries in SQL to unravel these structures.

The Challenge: Understanding Employee Levels

Imagine you’re handed a database table named employees. This table captures the structure of an organization, detailing each employee's immediate manager. Your task? To determine the hierarchical level of each employee within the organization.

Here’s a brief overview of the employees table:

  • id: Unique identifier for each employee.
  • first_name: Employee's first name.
  • last_name: Employee's last name.
  • manager_id: The ID of the employee's immediate manager. If this is NULL, the employee is at the topmost level (think CEO or President).

Your goal is to generate a result that includes:

  • level: The hierarchical level of the employee.
  • id: Employee's ID.
  • first_name: Employee's first name.
  • last_name: Employee's last name.
  • manager_id: The ID of the employee's manager.

The challenge? This needs to be achieved using a recursive query in SQL.

The Approach: Recursive CTEs to the Rescue

Common Table Expressions (CTEs) in SQL provide a way to create temporary result sets that can be easily referenced within a SELECT, INSERT, UPDATE, or DELETE statement. Recursive CTEs extend this capability, allowing us to iterate over the data until a specified condition is met.

For our problem, we’ll use a recursive CTE to traverse the employee hierarchy:

WITH RECURSIVE employee_levels AS (
-- Base case: Employees with no managers (level 1)
SELECT 1 AS level, id, first_name, last_name, manager_id
FROM employees
WHERE manager_id IS NULL

UNION ALL

-- Recursive case: Increment level for each subsequent employee
SELECT e1.level + 1, e2.id, e2.first_name, e2.last_name, e2.manager_id
FROM employee_levels e1
JOIN employees e2 ON e1.id = e2.manager_id
)

-- Select from the CTE
SELECT level, id, first_name, last_name, manager_id
FROM employee_levels
ORDER BY level, id;

Breaking Down the Query

  1. Base Case: We start by identifying employees with no managers (i.e., manager_id IS NULL). These employees are at level 1 in the hierarchy.
  2. Recursive Case: For each subsequent level, we join the employees table with our CTE (employee_levels) based on the manager_id. For each match, we increment the level by 1.
  3. Finally, we select from our CTE, ordering the results by level and id.

Conclusion

Recursive queries, especially when dealing with hierarchical data structures, offer a powerful tool in the SQL arsenal. By understanding and leveraging recursive CTEs, we can efficiently navigate and analyze complex hierarchies, gaining deeper insights into our data. For those keen on diving deeper into recursive queries and CTEs, here’s a comprehensive guide to get you started. If you have further questions or would like to connect, feel free to reach out to me on LinkedIn.

Happy querying!

  • SQL Recursive Queries
  • Hierarchical Data Structures
  • Common Table Expressions
  • Employee Hierarchy Analysis
  • Database Management
  • SQL Best Practices
  • Organizational Data Analysis

Subscribe to DDIntel Here.

DDIntel captures the more notable pieces from our main site and our popular DDI Medium publication. Check us out for more insightful work from our community.

Register on AItoolverse (alpha) to get 50 DDINs

Support DDI AI Art Series: https://heartq.net/collections/ddi-ai-art-series

Join our network here: https://datadriveninvestor.com/collaborate

Follow us on LinkedIn, Twitter, YouTube, and Facebook.

--

--