An employee hierarchy can be represented in one Employees table where each employee has a ManagerID referencing another employee's EmployeeID. A self JOIN connects employees with their managers.
Organizational structures are commonly stored in a single table and need hierarchical queries for reporting.
Display every employee along with the name of their direct manager.
SELECT
e.EmployeeID,
e.EmployeeName,
m.EmployeeName AS ManagerName
FROM Employees e
LEFT JOIN Employees m
ON e.ManagerID = m.EmployeeID;
Used in HR reporting, organizational charts, approval workflows, and management dashboards.
- ManagerID can reference EmployeeID.
- The same table can represent employees and managers.
- Aliases distinguish the two roles.
- LEFT JOIN preserves top-level employees.
- Self JOINs support hierarchy reporting.
How can employee-manager data be queried?
Use a self JOIN where ManagerID from one employee row matches EmployeeID of another employee row.
Why use LEFT JOIN for employee hierarchy?
It preserves employees who do not have a manager, such as the top-level employee.