N052-H2 Tier 4 · Advanced · hard hr · Helix Systems

Return the ID, name, and depth level for employee `id = 2` and every employee who reports to them at any level

Part of Recursive CTEs in SQL

The problem

Helix Systems' organizational restructuring tool identifies every employee within a specific subtree of the hierarchy. The subtree is rooted at employee id = 2.

Write a query to return the ID, name, and depth level for employee id = 2 and every employee who reports to them at any level.

Assumptions:

  • The employees table has one row per employee with an id, a name, and a manager_id.
  • Employee id = 2 is the root of the subtree and appears at depth 1. Direct reports of employee id = 2 are at depth 2, indirect reports at depth 3, and so on through every level beneath them.
  • Every employee in the subtree (including employee id = 2) appears once. Employees outside the subtree do not appear.

Output:

  • One row per subtree member, with columns id, name, and depth.
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
  subtree AS (
    SELECT
      id,
      name,
      1 AS depth
    FROM
      employees
    WHERE
      id = 2
    UNION ALL
    SELECT
      e.id,
      e.name,
      s.depth + 1
    FROM
      employees e
      JOIN subtree s ON e.manager_id = s.id
  )
SELECT
  id,
  name,
  depth
FROM
  subtree

The shape

The anchor seeds the recursion with the subtree root itself, and the recursive member walks downward by linking children to parents already in the CTE on manager_id = id. Because the anchor's filter is WHERE id = 2 — a single row — the recursion is bounded by the subtree of that one node; employees outside Marcus Reid's branch are never reachable from any row in the CTE.

Clause by clause

  • The anchor seeds the root of the subtree:
SELECT id, name, 1 AS depth
FROM employees
WHERE id = 2

One row enters subtree carrying depth = 1. Marcus Reid is included in the result because the anchor pulls his row in directly.

  • The recursive member walks downward through the subtree:
UNION ALL
SELECT e.id, e.name, s.depth + 1
FROM employees e
JOIN subtree s ON e.manager_id = s.id

The join e.manager_id = s.id matches each employee to a row already in subtree who is their manager. Pass one adds Anna Kim, Ben Walsh, Cara Lee, and Ora Price at depth 2 — the four employees whose manager_id is 2. Pass two adds the eleven employees who report to those four, all stamped depth = 3. Pass three finds no employees whose manager_id matches any depth-3 row in the subtree, so the recursion stops.

  • The final SELECT returns the subtree:
SELECT id, name, depth
FROM subtree

Sixteen rows: Marcus Reid at depth 1, four direct reports at depth 2, eleven indirect reports at depth 3.

Why anchor on a single node and not on direct reports

The prompt asks for the root and every descendant. Seeding the anchor with the root's row puts the root at depth 1 automatically; the recursion then handles the descendants. The alternative — seeding the anchor with the direct reports and then unioning the root in separately — produces the same result, but with an extra clause and an extra place for the depth numbering to drift out of alignment. Single-node anchor is the cleaner shape when "the root and everyone beneath" is the contract.

The trap

The subtree is bounded by the join graph, not by an explicit filter on the recursive member. Once subtree is seeded with employee 2, the only employees the recursive join can reach are those whose manager_id traces back to him through one or more steps. Employees outside Marcus's branch never enter the CTE because no row already in subtree ever matches their manager_id. This is structurally different from "compute the full hierarchy, then filter to one subtree," which materialises the whole tree first. The single-node anchor is the more efficient shape because the recursion does no work outside the subtree of interest.

You practiced a downward WITH RECURSIVE rooted at a specific node — the anchor seeds a single record (the subtree root); the recursion follows manager_id matches downward to every descendant.

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.