Top earner per department
MediumWindow functionsHR analytics
The problem
For each department, show the highest-paid employee(s). Return dept_name, name and salary.
If two people tie for the top salary, show both. Employees with no department are excluded.
Learn this in the course: Window functions (video and notes)
Tables · Company directory
Click a table or column name to insert it into your query.
- INTEGER · primary key
- TEXT
Sample rows (4 total)
| dept_id | dept_name |
|---|---|
| 1 | Engineering |
| 2 | Analytics |
| 3 | Sales |
| 4 | HR |
- INTEGER · primary key
- TEXT
- INTEGER · → departments, can be NULL
- INTEGER · → employees.emp_id, NULL for top managers
- INTEGER · rupees per year
Sample rows (12 total)
| emp_id | name | dept_id | manager_id | salary |
|---|---|---|---|---|
| 1 | Anita Rao | 1 | NULL | 260000 |
| 2 | Bhavin Shah | 1 | 1 | 180000 |
| 3 | Chitra Menon | 1 | 1 | 180000 |
| 4 | Deepak Verma | 1 | 2 | 140000 |
| 5 | Esha Gupta | 2 | NULL | 220000 |
Ctrl/⌘ + Enter runs the query. Table and column names are suggested as you type. Press Esc then Tab to leave the editor with the keyboard.
Expected output (3 rows)
| dept_name | name | salary |
|---|---|---|
| Engineering | Anita Rao | 260000 |
| Analytics | Esha Gupta | 220000 |
| Sales | Imran Sheikh | 170000 |
Row order doesn’t matter for this question. Numbers are compared with a tiny tolerance; text must match exactly.
Stuck?
Stored on this device only. Your latest query and “solved” status are remembered in this browser only. They don’t sync between devices, and clearing your browser data removes them.