RANK: Rank Employees by Salary (With Gaps)
SQL coding challenge · Difficulty: easy · Topic: Window Functions · +50 XP
Problem
HR is building a salary leaderboard where tied employees share the same rank — but the next rank skips a number *(like Olympic medals: 1st, 1st, 3rd)*.
Tables
Table: 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 |
Expected Output
| emp_id | emp_name | salary | rank_num | | 101 | John | 90000 | 1 | | 104 | David | 80000 | 2 | | 102 | Alice | 75000 | 3 | | 103 | Bob | 75000 | 3 | | 105 | Emma | 65000 | 5 |
- Return:
emp_id,emp_name,salary,rank_num - Tied employees share the same rank, next rank skips
- Sort by
salarydescending - Function to use:
RANK()
What this SQL challenge teaches you
“RANK: Rank Employees by Salary (With Gaps)” is a easy-level SQL challenge focused on Window Functions. Working through it gives you hands-on practice with RANK, OVER, ORDER BY, Gaps in ranking — 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
- OVER
- ORDER BY
- Gaps in ranking
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:
- Approach: use RANK().
- Your result should return: emp_id, emp_name, salary, rank_num.
- Order the output salary descending.
- Compare your output to the Expected Output — the columns, values and row order must match exactly.
Where this comes up
Variations of this problem have been reported in interviews at Amazon, Google, Microsoft, Flipkart. 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
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.