HardSQL challenge #212SELF JOINLEFT JOINHIERARCHY

Find Each Employee's Level in the Hierarchy with Self Joins

Return every employee with their level: 1 for people with no manager, 2 for their reports, 3 for the level below.

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 level of each employee in the hierarchy: 1 for people with no manager, 2 for their direct reports, 3 for the level below.

A correct answer returns 120 rows with 3 columns: id, manager_id, level.

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
idmanager_idlevel
1NULL1
2NULL1
3NULL1
4NULL1
5NULL1

First 5 of 120 rows. This is the output of the model solution on the practice database. The task does not fix a sort order, so your rows may come back in a different order.

What you practise

  • Testing for missing values with IS NULL and IS NOT NULL
  • Keeping every row of the left table with LEFT JOIN, matched or not
  • Joining a table to itself under two aliases
  • 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

Self join employees to their manager and to the manager's manager, then count how many of those exist

Show the step-by-step approach

How the model solution is built, in the order the database evaluates it:

  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).

Model solution

Checking your session…

What to watch for

Each LEFT JOIN climbs one step up the chain, and a comparison such as m.id IS NOT NULL evaluates to 1 or 0 in SQLite, so adding them counts the managers above. This handles a fixed number of levels; an unknown depth needs WITH RECURSIVE, which the Playground does not allow. This dataset has two levels.

  • 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.
  • Calculated columns. Dividing one integer by another truncates the result in SQLite. Multiply one side by 1.0 to get decimals.
Solve This SQL Challenge

Same technique, other tables

More practice on this pattern with different columns:

Keep learning