Self Joins & Hierarchical Queries SQL Topic exerciseHardVerified answerSQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

Total Payroll Under Each Manager (Subtree Salary Sum)

For every employee who is a manager (has at least one report), compute the total payroll of their entire subtree, INCLUDING the manager's own salary. Use a recursive (root, descendant) CTE on employees. Return manager_id, manager_name, subtree_size, total_payroll — ordered by total_payroll DESC, manager_id.

  • Recursive CTE
  • CTEs
  • Joins
  • Subqueries
  • Aggregation

Exercise brief

Understand the request

Workforce finance partner Leadership needs the payroll and population controlled by each manager across the full reporting subtree.

Return

  • Return manager identity, subtree size, and total payroll.
  • Order by payroll descending and manager_id.

Constraints

  • Include the manager in their own subtree.
  • Return only employees who have at least one report.

Data you will use

Review the relevant tables before deciding how to join, filter, or aggregate them.

employees

  • employee_idINTEGER
  • employee_nameTEXT
  • manager_idINTEGER
  • salaryINTEGER

Hints, when you need them

Open one clue at a time so you still do the reasoning.

Hint 1

subtree CTE = (root, descendant) pairs over the org tree. Same shape as Q18.

Hint 2

Filter to managers only with EXISTS (...there is at least one report...).

Hint 3

subtree_size INCLUDES the manager themselves; subtract 1 if you want "people under" only.

Verified SQL answer

Attempt the problem first, then compare structure and reasoning—not just syntax.

Reveal solution and explanation
WITH RECURSIVE subtree AS (SELECT employee_id AS root_id, employee_id AS desc_id FROM employees UNION ALL SELECT s.root_id, e.employee_id FROM employees e INNER JOIN subtree s ON e.manager_id = s.desc_id) SELECT m.employee_id AS manager_id, m.employee_name AS manager_name, COUNT(s.desc_id) AS subtree_size, SUM(emp.salary) AS total_payroll FROM employees m INNER JOIN subtree s ON s.root_id = m.employee_id INNER JOIN employees emp ON emp.employee_id = s.desc_id WHERE EXISTS (SELECT 1 FROM employees r WHERE r.manager_id = m.employee_id) GROUP BY m.employee_id, m.employee_name ORDER BY total_payroll DESC, m.employee_id;

Why this works

Subtree salary sums are exactly how 'cost center totals' are computed in production. The (root, descendant) shape lets one query compute totals at every level of the org chart simultaneously.

Success check

Each manager appears once with all direct and indirect reports counted exactly once.

Expected result

Use this output to verify values, aliases, ordering, and row count.

manager_idmanager_namesubtree_sizetotal_payroll
1Alice CEO151777000
2Bob VP Sales6642000
3Carol VP Eng5610000
5Emma Sales Mgr5492000
4David VP HR3325000
6Frank Eng Mgr3315000
7Grace HR Mgr2185000

Learn the concepts behind this answer

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Practice Online

Open the interactive workspace and practice across SQL topics.