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_idINTEGERemployee_nameTEXTmanager_idINTEGERsalaryINTEGER
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_id | manager_name | subtree_size | total_payroll |
|---|---|---|---|
| 1 | Alice CEO | 15 | 1777000 |
| 2 | Bob VP Sales | 6 | 642000 |
| 3 | Carol VP Eng | 5 | 610000 |
| 5 | Emma Sales Mgr | 5 | 492000 |
| 4 | David VP HR | 3 | 325000 |
| 6 | Frank Eng Mgr | 3 | 315000 |
| 7 | Grace HR Mgr | 2 | 185000 |
Learn the concepts behind this answer
Strengthen your understanding with these targeted learning topics:
Continue practicing
Build the next SQL skill
SQL Joins
Practice reliable INNER, LEFT, FULL, CROSS, self, semi, anti, range, temporal, and many-to-many join patterns.
CTEs & Window Functions
Practice modular CTE pipelines, deterministic window analytics, period comparisons, deduplication, frames, and gaps-and-islands.
SQL Subqueries
Practice scalar, derived-table, correlated, EXISTS, NULL-safe anti-subquery, quantified, and row-subquery patterns.
Open the interactive workspace and practice across SQL topics.