Who's the boss around here?
- O(n log n) · Hard
- Full plan
- SQL
- joins
- grouping
Problem
At this company, being a boss isn't a role: a boss is anyone who has someone reporting to them. HR wants to know how many people each boss has and what those people's salaries add up to.
Show the name of each person with someone reporting to them, how many people report to them directly, and the sum of those people's salaries, in that order.
Sort from the highest sum to the lowest; on a tie, by name.
What it has to return
These rows, in this order
| name | people | payroll |
|---|---|---|
| Carol | 3 | 10300 |
| Paige | 3 | 8600 |
| Mark | 2 | 4000 |
This is what comes out with the data in the practice database this challenge uses. The column names don’t count, so name yours however you like: what counts is how many there are and the order they come in. Besides this one, the challenge has hidden tests with other data that are revealed when you submit your solution.
You start with this
SQL
SELECT b.name
FROM employees e
JOIN employees b
ON e.boss_id = b.idIt opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.