What the team makes
- O(n) · Medium
- Full plan
- SQL
- grouping
- joins
- decimals
Problem
Anyone applying for a job asks what people make in that department, and they aren't asking about the manager's paycheck. HR wants the number on hand: each department's average monthly salary, leaving out whoever has the role Manager.
Show the department's name, how many people go into the calculation, and their average salary, rounded to 2 decimals, in that order. Where there's no manager, everyone in the department counts. And where only the manager is left, or nobody works there, there's no team to average: that department isn't on the list.
What it has to return
These rows, in any order
| name | people | average |
|---|---|---|
| Sales | 2 | 2000 |
| Tech | 3 | 2866.67 |
| Design | 1 | 2400 |
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.role,
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
- Where are the books from?SQL · grouping · joins · sorting
- Where do our orders come from?SQL · grouping · joins · nulls
- Where there's more than oneSQL · grouping · nulls
- Who owes grades?SQL · grouping · joins · nulls
- A two-credit 10SQL · grouping · joins · decimals
- Aisle by aisleSQL · grouping · conditionals