ROW_NUMBER: Assign Unique Rank to Each Employee

SQL coding challenge · Difficulty: easy · +50 XP

Problem

Your data team needs a unique sequential row number for every employee ordered by salary (highest first). Used in the internal salary transparency report.

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 | department | salary | row_num |
| 101 | John | IT | 90000 | 1 |
| 104 | David | HR | 80000 | 2 |
| 102 | Alice | IT | 75000 | 3 |
| 103 | Bob | IT | 75000 | 4 |
| 105 | Emma | HR | 65000 | 5 |
  • Return: emp_id, emp_name, department, salary, row_num
  • No two rows share the same number — break ties by emp_id ascending
  • Sort by salary descending
  • Function to use: ROW_NUMBER()

Solve this challenge on PySpark.in