HardSQL challenge #293SELF JOINLEFT JOINHIERARCHY

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.

Solve This SQL Challenge

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

First 3 rows of the employees table
idfirst_namelast_nameemaildepartmentdepartment_idtitlesalaryhire_datemanager_idcitystatus
1WilliamLewiswlewis18@corp.ioCustomer Success7Senior CS Manager617742022-07-24NULLSan Diegoactive
2JohnMillerjmiller51@corp.ioLegal8Paralegal1916942021-08-09NULLSan Diegoactive
3DanielRiveradrivera50@corp.ioEngineering1VP Engineering1350672016-06-06NULLHoustonactive

The first rows of each table are shown. See how all the tables relate on the database schema page.

Expected output

Expected output of the challenge
depth
2

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:

  1. Build an inner query first (a derived table):
    1. Read from the employees table, under the alias e.
    2. LEFT JOIN employees again, under the alias m, on e.manager_id = m.id. Rows from the left side that have no match are kept, with NULL in the joined columns.
    3. LEFT JOIN employees again, under the alias g, on m.manager_id = g.id. Rows from the left side that have no match are kept, with NULL in the joined columns.
    4. Add the calculated column level: 1 + (m.id IS NOT NULL) + (g.id IS NOT NULL).
  2. 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.
Solve This SQL Challenge

Keep learning