Kodewick
Start learning →
Starter→0 / 50

Top earner in each department

#001StarterROW_NUMBER() OVER (PARTITION BY …)6 min

HR wants to see who earns the most in each department. Using employees and departments, list the best-paid employee of each department: the department name, the employee's name and the salary. Employees with no department don't count.

The tables
departments
  • idPK
  • name
employees
  • idPK
  • name
  • dept_idFK
  • manager_id
  • salary
  • hire_date

employees.dept_id → departments.id

departments
idname
1Sales
2Engineering
3Support
4Legal
4 rows
employees
idnamedept_idmanager_idsalaryhire_date
1Marta2NULL90002019-03-01
2Paulo1170002020-01-10
3Lucas2182002020-06-15
4Sofia2195002021-02-01
5Rafael2360002023-09-04
6Julia1272002021-11-20
7Tiago1250002024-03-11
8Ines1250002024-07-01
9Nuno3145002020-09-30
10Clara3942002022-05-16
11Pedro3948002023-01-09
12VeraNULL139002025-02-03
12 rows
query.sql
-- write your query here
Ctrl + Enter runs too
Result

Run shows what your query gives. When it looks right, Submit.