Out of every 100 in payroll
- O(n²) · Very hard
- Full plan
- SQL
- window functions
- decimals
Problem
Management wants to see how much of the payroll sits at the top. Out of every 100 the company pays in salaries, how much goes to each person? And how much goes, all together, to everyone who earns as much as that person or more?
Show the name, the salary, that person's share of the whole company's payroll, and the combined share of everyone who earns that salary or more, that person included, in that order. Both shares are percentages, rounded to 1 decimal. Two people with the same salary get the same numbers. Carol has no department, but she gets paid: her salary counts.
Sort from the highest salary to the lowest and, on a tie, by name.
What it has to return
These rows, in this order
| name | salary | share | top_share |
|---|---|---|---|
| Carol | 5200 | 18.5 | 18.5 |
| Paige | 4100 | 14.6 | 33.1 |
| Mark | 3800 | 13.5 | 46.6 |
| Lydia | 3000 | 10.7 | 68 |
| Samuel | 3000 | 10.7 | 68 |
| Frank | 2600 | 9.3 | 77.2 |
| Grace | 2400 | 8.5 | 85.8 |
| Nicole | 2100 | 7.5 | 93.2 |
| Rachel | 1900 | 6.8 | 100 |
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 name, salary,
SUM(salary) OVER () AS total
FROM employees
ORDER BY salary DESC, name;It 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.
More O(n²) challenges
- Paige is leavingSQL · changing data · subqueries
- Past dueSQL · dates · nulls · joins
- Starting at the bottomSQL · changing data · subqueries
- The library forgetsSQL · changing data · subqueries
- The next rung upSQL · window functions · subqueries
- The register, day by daySQL · window functions · grouping · conditionals