runtime
36 / 88

task GROUP BY · medium

Payroll versus budget

For every department, return department, headcount, payroll (sum of salaries) and status: 'over' when payroll exceeds the budget, otherwise 'within'.

what to return

Columns
departmentheadcountpayrollstatus in this order
Rows
6 rows · any order
Watch for
Departments with no employees show headcount 0 and payroll 0.
Payroll equal to the budget is 'within'.
Skill
Report every group, including empty ones, and classify it. Refresher: Make decisions with CASE →

expected result

Your query should return exactly these columns and values. Run it and compare.

expected result6 rows · any order
departmentheadcountpayrollstatus
Engineering71000000within
Sales4490000over
Finance2215000within

first 3 of 6 rows

data you can query

Sample rows from each table. Each column shows its type underneath; → marks a link to another table.

departments4 columns · 6 rows
idinteger · keynametextlocationtextbudgetreal
1EngineeringLisbon1100000
2SalesAustin450000
3FinanceAustin250000
4PeopleLisbon150000
5DesignRemote305000

showing 5 of 6 rows

employees7 columns · 20 rows
idinteger · keynametextmanager_idinteger → employeesdepartment_idinteger → departmentstitletextsalaryrealhired_attext
1Ada MorenoNULLNULLCEO2500002019-03-01
2Bruno Costa11VP Engineering1800002019-06-15
3Carmen Diaz12VP Sales1500002020-01-10
4Dev Patel21Engineering Manager1400002020-05-04
5Elif Kaya41Senior Engineer1450002021-02-01

showing 5 of 20 rows

solution.sql
 

Hints and the answer are always available. Using them counts the question as solved with help.