Querydrill

Level 7: Case studies Exercise 159 of 161 Hard Shares and rates

Payroll review, step 3: the top earner's share

An SQL exercise from Querydrill. Solve it with a SQL editor in your browser, checked on the rows it returns and again on changed data, and it is checked the moment you submit.

Try it yourself

How concentrated is the pay? For each department, return the department name, the name of its highest-paid employee and that salary as a percentage of the department's total salary, rounded to 1 decimal. If several employees share the top salary, return each of them.

departments (4 rows)

idname
1Sales
2Engineering
3Support
4Finance

employees (11 rows)

idnamedept_idsalaryhire_datemanager_id
1Ava21300002019-03-15NULL
2Ben21050002020-07-011
3Cleo1820002021-01-107
4Dan1760002022-05-237
5Eve3540002022-09-1211
6Finn2980002021-11-301
7Gia1990002018-06-04NULL
8Hugo3610002020-02-18NULL
9Iris4880002019-10-07NULL
10Jack4720002023-04-179
11KaiNULL500002024-01-088

Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.

What this exercise teaches

Top per group with ties, plus a figure for the whole group, is a two-step query: aggregate per department, then join back to pick up the rows that match. The window version computes RANK and SUM over the same partition in one pass.

A hint if you get stuck

First one row per department with its top salary and its total; then join the employees who earn that top salary.

Background you need

Common mistakes on this exercise

  • ROW_NUMBER returns one employee when two share the top salary.
  • Share of the company payroll, not of the department's.

Each of these returns a result that looks plausible. The checker catches them because it runs your query on changed data as well.

Keep going on the practice page

Your first 8 exercises on Querydrill are free, with no account. The practice page has the full editor, timed mode and all the exercises in order. A free account shows 3 reference solutions a day; Pro unlocks every exercise, every solution, timed mode and the certificate.

Open this exercise

More practice on the same topic

More exercises in Case studies