Employee Hierarchy Depth
Find the maximum depth of the employee hierarchy (1 means nobody has a manager).
The challenge runs on the SQLabHub practice database (SQLite) and uses the employees table. You can solve it in the browser, with nothing to install.
The task
Find the maximum depth of the employee hierarchy (1 means nobody has a manager).
A correct answer returns 1 row with one column: depth.
Tables used in this challenge
employees 120 rows
id INTEGER (PK) · first_name TEXT · last_name TEXT · email TEXT · department TEXT · department_id INTEGER · title TEXT · salary REAL · hire_date TEXT · manager_id INTEGER · city TEXT · status TEXT
| id | first_name | last_name | department | department_id | title | salary | hire_date | manager_id | city | status | |
|---|---|---|---|---|---|---|---|---|---|---|---|
The first rows of each table are shown. See how all the tables relate on the database schema page.
Expected output
| depth |
|---|
The complete result: 1 row. This is the output of the model solution on the practice database.
What you practise
- Testing for missing values with IS NULL and IS NOT NULL
- Summarising many rows into one value with COUNT, SUM, AVG, MIN or MAX
- Keeping every row of the left table with LEFT JOIN, matched or not
- Joining a table to itself under two aliases
- Querying the result of another query with a subquery in FROM
- Deriving a new column from an expression and naming it with an alias
Hint and approach
Try the challenge first. Open these only when you need them.
Show the hint
Compute each employee's level with self joins, then take the maximum
Show the step-by-step approach
How the model solution is built, in the order the database evaluates it:
- Build an inner query first (a derived table):
- Read from the
employeestable, under the aliase. - LEFT JOIN
employeesagain, under the aliasm, one.manager_id=m.id. Rows from the left side that have no match are kept, with NULL in the joined columns. - LEFT JOIN
employeesagain, under the aliasg, onm.manager_id=g.id. Rows from the left side that have no match are kept, with NULL in the joined columns. - Add the calculated column
level:1 + (m.id IS NOT NULL) + (g.id IS NOT NULL).
- Read from the
- Aggregate over all the rows that are left: compute
MAX(level). With no GROUP BY, the result is a single row.
Model solution
Checking your session…
What to watch for
- Derived table. Columns computed in the inner query are only visible outside under their alias, so every calculated column there needs a name.
- Self join. Without a condition such as a.id < b.id, a self join pairs every row with itself and lists each pair twice.
- LEFT JOIN. A WHERE condition on the right-hand table removes the unmatched rows and turns the LEFT JOIN back into an inner join. Conditions on that table go in the ON clause.
Related SQL challenges
- Find Each Employee's Level in the Hierarchy with Self JoinsSimilar levelHard
- Compare Each Employee's Salary with Their Manager'sSimilar levelHard
- List Employees with Their Manager Names Using a Self JoinEasierMedium
- Count Direct Reports per ManagerSimilar levelHard
- Find Customers Who Never Placed an OrderSimilar levelHard
Keep learning
- Lesson: SQL Subqueries
- Lesson: SELF JOIN
- Lesson: LEFT JOIN
- Lesson: Comments & Aliases
- All SQL practice challenges, by difficulty and topic
- SQL interview questions with answers
- SQL Playground: run any query on the same database