Top 3 Highest-Paid Employees in Each Department
SQL coding challenge · Difficulty: medium · Topic: Window Functions · +100 XP
Problem
The annual bonus pool is shared among the **three highest earners in each
department**. Find them.
Engine: queries run on MySQL 8.0, so use MySQL-compatible syntax and functions.
Tables
employees — one row per employee.
| column | type | description | | --- | --- | --- | | employee_id | INT | identifies the employee | | first_name | VARCHAR | the employee's name | | department_id | INT | department they belong to | | salary | INT | annual salary | | hire_date | DATE | date they joined | | manager_id | INT | their manager, NULL for the department head |
departments — one row per department.
| column | type | description | | --- | --- | --- | | department_id | INT | identifies the department | | department_name | VARCHAR | the department's name |
Return, for each department, its three highest-paid employees:
- department_id
- employee_id
- first_name
- salary
Sort by department_id ascending, then salary descending, then
employee_id ascending.
Ties are included, and that changes the count
Salaries repeat, so "top 3" is not always three rows. Rank employees by salary
within their department, and keep everyone whose rank is 3 or better:
- If two people tie for 3rd, both are in and the department returns four
rows. Picking an arbitrary three would drop a person who earns exactly as much
as the one you kept.
- If two people tie for 2nd, they occupy 2nd and 3rd place between them, so
the next distinct salary is 4th and does not qualify — that department
returns three rows. A tie consumes the places it spans.
A department with fewer than three employees simply returns all of them. A
department with no employees at all does not appear in the result, even though
it exists in departments.
Example input
departments
| department_id | department_name | | --- | --- | | 1 | Engineering | | 2 | Marketing | | 3 | HR | | 4 | Finance |
employees
| employee_id | first_name | department_id | salary | hire_date | manager_id | | --- | --- | --- | --- | --- | --- | | 1 | Alice | 1 | 90000 | 2021-03-10 | NULL | | 2 | Bob | 1 | 75000 | 2021-03-20 | 1 | | 3 | Charlie | 1 | 80000 | 2021-03-25 | 1 | | 4 | Victor | 1 | 75000 | 2022-01-15 | 1 | | 5 | Diana | 2 | 70000 | 2022-06-05 | 1 | | 6 | Eve | 2 | 65000 | 2021-11-15 | 5 | | 7 | Frank | 3 | 60000 | 2023-02-28 | 1 |
Expected output
| department_id | employee_id | first_name | salary | | --- | --- | --- | --- | | 1 | 1 | Alice | 90000 | | 1 | 3 | Charlie | 80000 | | 1 | 2 | Bob | 75000 | | 1 | 4 | Victor | 75000 | | 2 | 5 | Diana | 70000 | | 2 | 6 | Eve | 65000 | | 3 | 7 | Frank | 60000 |
Engineering returns four rows: Alice is 1st, Charlie 2nd, and Bob and Victor
both earn 75000 so they share 3rd — both qualify. Marketing has only two
employees, so both are returned. HR has one. Finance appears in departments
but employs nobody, so it is absent from the result entirely.
What this SQL challenge teaches you
“Top 3 Highest-Paid Employees in Each Department” is a medium-level SQL challenge focused on Window Functions. Working through it gives you hands-on practice with RANK, PARTITION BY, TOP-N per group, Subquery — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- RANK
- PARTITION BY
- TOP-N per group
- Subquery
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- Ranking happens per department, so the window needs PARTITION BY department_id with the rows ordered by salary descending.
- Pick the ranking function deliberately. ROW_NUMBER() always yields exactly three rows and would silently drop someone tied for 3rd. You want tied employees to share a place.
- The other trap is the opposite one: after a tie for 2nd, the next distinct salary should be 4th, not 3rd. A function that closes the gap lets an extra person in.
- A window function cannot be used in WHERE, because WHERE runs before the window is computed. Compute the rank in a subquery or CTE and filter on it in the outer query.
- Departments with fewer than three employees return all of them, and a department with no employees produces no rows at all -- there is nothing to rank, so no join to departments is needed.
Where this comes up
Variations of this problem have been reported in interviews at Amazon, Google, Databricks, Netflix. Interviewers use it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.
How to practise it on PySpark.in
Open the challenge, write your SQL query in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related SQL challenges
- Top 3 Products per Category
- Running Total Revenue
- Median Salary per Department
- Latest Order Per Customer
- 3-Day Rolling Sum of Sales
- 7-Day Rolling Purchase Amount by Customer
Helpful resources
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The SQL environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated medium and covers Window Functions.