Design database for employee hierarchy

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2023-09-28

Problem Statement

Design a database system to store and query employee hierarchy within an organization. The system should efficiently support hierarchical queries such as reporting lines, team structures, and managerial relationships.


Functional Requirements (FR)


Employee Management


•Store employee information: name, employee ID, designation, department, manager, and joining date.
•Add, update, or remove employees.


Hierarchy Management


•Maintain reporting relationships (manager → subordinates).
•Support multiple levels of hierarchy.
•Identify top-level managers (CEO/Head of Department).


Querying & Reporting


•Retrieve all subordinates of a manager.
•Find direct manager of an employee.
•Retrieve complete reporting chain (employee → manager → top-level).
•Find common manager between two employees.


Departments & Teams (Optional)


•Group employees by department or team.
•Query employees within a department or under a team lead.


Access & Permissions (Optional)


•Restrict certain hierarchy data based on role (HR/Admin).


Non-Functional Requirements (NFR)


•Scalability – Support thousands of employees and multiple hierarchy levels.
•Performance – Fast retrieval for hierarchical queries.
•Consistency – Ensure reporting relationships are accurate.
•Extensibility – Easily add new attributes, roles, or departments.


Core Entities & Attributes


Entity Attributes


Employee employeeId (PK), name, designation, departmentId, managerId (FK to Employee), joiningDate, contactInfo

Department departmentId (PK), name, headEmployeeId (FK to Employee)

Role (Optional) roleId (PK), roleName, permissions


Design Approaches


Adjacency List (Simplest)


•Each employee record stores managerId pointing to their manager.
•Pros: Simple to implement.
•Cons: Recursive queries needed for full hierarchy (may require multiple joins).


Nested Set Model


•Store left and right values to represent hierarchy.
•Pros: Efficient for retrieving subtrees.
•Cons: Expensive updates when hierarchy changes.


Materialized Path


•Store full path as a string (e.g., /CEO/CTO/Manager/Employee).
•Pros: Easy to query subordinates using string matching.
•Cons: Path updates needed when hierarchy changes.


Graph / Recursive Queries (Modern DBs)


•Use graph databases (Neo4j) or relational DBs with recursive CTEs.
•Pros: Flexible, easy to query complex relationships.
•Cons: May require specialized DB knowledge.


Key Queries


Direct Subordinates

1 2SELECT * FROM Employee WHERE managerId = :managerId;
1.All Subordinates (Recursive)
1 2WITH RECURSIVE subordinates AS ( 3 SELECT employeeId, name FROM Employee WHERE managerId = :managerId 4 UNION ALL 5 SELECT e.employeeId, e.name 6 FROM Employee e 7 INNER JOIN subordinates s ON e.managerId = s.employeeId 8) 9SELECT * FROM subordinates;
1.Reporting Chain for Employee
1 2WITH RECURSIVE managers AS ( 3 SELECT employeeId, managerId FROM Employee WHERE employeeId = :employeeId 4 UNION ALL 5 SELECT e.employeeId, e.managerId FROM Employee e 6 INNER JOIN managers m ON e.employeeId = m.managerId 7) 8SELECT * FROM managers;
1.Find Common Manager Between Two Employees
•Retrieve reporting chains for both employees and find intersection.


Design Considerations


1.Hierarchical Queries
•Optimize for frequent reporting queries using indexes or materialized views.
2.Updates & Promotions
•Ensure changes in managerId propagate correctly.
3.Extensibility
•Add support for multiple reporting managers or dotted-line reporting if required.
4.Performance
•Index on managerId for quick subordinate lookups.
•Consider caching for frequently accessed hierarchies.


✅ This HLD problem tests:


•Ability to model hierarchical data in relational or graph databases.
•Understanding of recursive queries and tree traversal techniques.
•Trade-offs between query performance and update complexity.


Solution

Comments