How many payrolls does it cover?
- O(n) · Medium
- Full plan
- SQL
- grouping
- joins
- decimals
Problem
Management hands out the budget and has never set it next to what each department pays in salaries. For each department where someone works, show its name, its budget, its payroll (the sum of its people's monthly salaries), and how many times that payroll goes into its budget, rounded to 1 decimal, in that order.
An empty department has no payroll to compare with, and someone who doesn't belong to any department isn't on anyone's payroll.
What it has to return
These rows, in any order
| name | budget | payroll | times |
|---|---|---|---|
| Sales | 50000 | 7800 | 6.4 |
| Tech | 80000 | 12700 | 6.3 |
| Design | 30000 | 2400 | 12.5 |
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 d.name, e.salary
FROM employees e
JOIN departments d
ON e.dept_id = d.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.
More O(n) challenges
- Not the priciest, not the biggestSQL · grouping · joins
- Pages carried homeSQL · grouping · joins · decimals
- Same book twice doesn't countSQL · grouping · sorting
- The 250 ClubSQL · grouping · joins · decimals
- The notebook by the registerSQL · grouping · joins
- What the team makesSQL · grouping · joins · decimals