ROW_NUMBER: Find the Highest-Paid Employee in Each Department

SQL coding challenge · Difficulty: easy · Topic: Window Functions · +50 XP

Problem

Every department head wants their top earner on the team dashboard. Return

exactly one row per department: the highest-paid employee in it.

Engine: queries run on MySQL 8.0, so use MySQL-compatible syntax and functions.

Tables

employees_wf — one row per employee.

| column | type | description |
| --- | --- | --- |
| emp_id | INT | identifies the employee |
| emp_name | VARCHAR | the employee's name |
| department | VARCHAR | department they work in |
| salary | INT | annual salary |

Return, for each department, its highest-paid employee:

Sort the result by department ascending.

Exactly one row per department, ties included

A department can have two people on the same top salary. The dashboard still

has room for one name, so the result must never contain two rows for the

same department — and which one appears cannot be left to chance, or the same

data would produce different dashboards on different days.

Break the tie on emp_id ascending: among employees tied for the highest

salary in a department, the one with the lowest emp_id is returned.

Put that tiebreaker inside the window's own ORDER BY, alongside the salary.

Ordering the final result does not help — by then the row has already been

chosen, and the losing row has already been discarded.

Every department that has at least one employee contributes exactly one row.

Example input

employees_wf

| emp_id | emp_name | department | salary |
| --- | --- | --- | --- |
| 101 | John | IT | 90000 |
| 102 | Alice | IT | 75000 |
| 103 | Bob | IT | 75000 |
| 104 | David | HR | 80000 |
| 105 | Emma | HR | 65000 |
| 106 | Grace | Finance | 70000 |
| 107 | Henry | Finance | 70000 |

Expected output

| emp_id | emp_name | department | salary |
| --- | --- | --- | --- |
| 106 | Grace | Finance | 70000 |
| 104 | David | HR | 80000 |
| 101 | John | IT | 90000 |

Three departments, three rows. John tops IT on 90000 and David tops HR on 80000,

both outright. Finance is the interesting one: Grace and Henry both earn

70000, so salary alone cannot separate them — Grace is returned because 106 is

lower than 107.

Note that Alice and Bob also share a salary in IT, but neither is their

department's top earner, so the tie never comes into play for them.

What this SQL challenge teaches you

“ROW_NUMBER: Find the Highest-Paid Employee in Each Department” is a easy-level SQL challenge focused on Window Functions. Working through it gives you hands-on practice with ROW_NUMBER, PARTITION BY, 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

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:

  1. Number the employees within each department, then keep only the first of each. PARTITION BY department restarts the numbering per department.
  2. Order the window by salary descending so the top earner is number 1.
  3. Salary alone does not settle a tie for top earner, so the winner would change between runs. Add emp_id ASC as a second key INSIDE the OVER clause.
  4. A window function cannot be used in WHERE -- WHERE runs before the window is computed. Compute the number in a subquery or CTE, then filter on it in the outer query.
  5. ROW_NUMBER() is the right family here because the result must hold exactly one row per department. A function that lets tied rows share a number would return two names for a department that ties at the top.

Where this comes up

Variations of this problem have been reported in interviews at Amazon, Microsoft, Infosys, TCS. 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

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 easy and covers Window Functions.

Solve this challenge free on PySpark.in