N052-M2 Tier 4 · Advanced · medium hr · Helix Systems

Return every depth level and the number of employees at that level

Part of Recursive CTEs in SQL

The problem

Helix Systems' workforce planning tool analyzes how headcount is distributed across the levels of the organizational hierarchy.

Write a query to return every depth level and the number of employees at that level.

Assumptions:

  • The employees table has one row per employee with an id and a manager_id.
  • The CEO has a missing manager_id and sits at depth 1. Employees one step below the CEO sit at depth 2, and so on.
  • Each depth level with at least one employee should appear once. The headcount at each level is the number of employees whose computed depth equals that level.

Output:

  • One row per depth level, with columns depth and employee_count.
Schema · hr 4 tables
departments
id integer
name text
location text
budget numeric
salaries
id integer
employee_id integer
amount numeric
effective_date date
end_date? date
employees
id integer
name text
email text
department_id integer
manager_id? integer
hire_date date
title text
is_active boolean
job_history
id integer
employee_id integer
title text
department_id integer
start_date date
end_date? date

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution Try it yourself first
Solution query
WITH RECURSIVE
  org AS (
    SELECT
      id,
      1 AS depth
    FROM
      employees
    WHERE
      manager_id IS NULL
    UNION ALL
    SELECT
      e.id,
      o.depth + 1
    FROM
      employees e
      JOIN org o ON e.manager_id = o.id
  )
SELECT
  depth,
  COUNT(id) AS employee_count
FROM
  org
GROUP BY
  depth

The shape

The recursive CTE produces one row per employee carrying their computed depth, and the outer GROUP BY depth then counts the rows at each level. The traversal is the same root-to-leaves walk used to assign depths; the aggregation is a follow-up step over its output.

Clause by clause

  • The anchor seeds the CEO at depth 1:
SELECT id, 1 AS depth
FROM employees
WHERE manager_id IS NULL

Only id and depth are carried through the recursion, since the final aggregation only needs the count per level. Dropping name and manager_id from the projection is a small efficiency.

  • The recursive member adds each subsequent level:
UNION ALL
SELECT e.id, o.depth + 1
FROM employees e
JOIN org o ON e.manager_id = o.id

The join condition is the same parent-to-child link as the unbounded tree walk. Each pass stamps the newly-added rows with one more than the source depth. The recursion ends when no further employee can be joined to a row already in org.

  • The main query aggregates over the result of the recursion:
SELECT depth, COUNT(id) AS employee_count
FROM org
GROUP BY depth

GROUP BY depth partitions org's rows into one bucket per level, and COUNT(id) counts the rows in each bucket. Depth 1 has one employee (the CEO), depth 2 has nine, depth 3 has nineteen, depth 4 has thirty-one.

Why an aggregation outside the recursion and not inside

The recursive member cannot use an aggregate over the CTE: an aggregate would collapse the rows the next pass needs to join against, and the recursion would lose its per-row structure. The right shape for "compute X per row recursively, then summarise" is two stages: let the recursion produce one row per record with the per-record value attached, then aggregate that flat result with a standard GROUP BY in the main query.

The trap

The depth column inside the recursion is a per-row attribute, not an aggregate. COUNT(id) in the outer query counts rows whose depth matches; it does not count distinct depth values or sum depths. Confusing the role of depth (a partition key in the outer GROUP BY) with the role it plays inside the recursion (a running counter on each row) is the easiest reading mistake on a query that mixes recursion and aggregation. The two-stage layering keeps each role legible.

You practiced an aggregate over the output of a WITH RECURSIVE traversal — recurse first to compute depth per record, then group by depth in the main query for a level-wise headcount.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.

Practice, feedback, mastery, review. That's the loop that turns reading into real skill.

Start free

No account, no credit card. Start solving in under a minute.