Skip to main content
Admissions guidance, study banks, and career tools — get started on Shalyam

Employee Hierarchy Levels

Using a recursive CTE, return every employee with their level in the hierarchy (the top employee with no manager is level 0). Return id, name and level ordered by level.

Table schema

Employees(id INT PRIMARY KEY, name VARCHAR(50), manager_id INT NULL)

Sample data

id | name | manager_id
1 | CEO | NULL
2 | VP | 1
3 | Manager | 2
4 | Dev | 3

Discussion

No discussions yet.

Rewards · v2.1

Get Exclusive Insights with Every Issue

Join Shalyam Newsletter

Stay ahead in education, research, and innovation—straight to your inbox.