SQL
All Top 50 SQL Interview Questions#41
ROW_NUMBER vs RANK vs DENSE_RANK in SQL Explained
MediumAmazonInterview Question #41
Asked at AmazonGiven an `employee` table with duplicate salary values, write a query to compute `ROW_NUMBER()`, `RANK()`, and `DENSE_RANK()` ordered by `salary DESC`. Contrast the resulting rankings.
Input Table: employee
4 rows preview| emp_name | salary |
|---|---|
| Alice | 10000 |
| Bob | 9000 |
| Charlie | 9000 |
| Dave | 8000 |
Expected Output Structure4 rows
| emp_name | salary | row_num | rank_val | dense_rank_val |
|---|---|---|---|---|
| Alice | 10000 | 1 | 1 | 1 |
| Bob | 9000 | 2 | 2 | 2 |
| Charlie | 9000 | 3 | 2 | 2 |
| Dave | 8000 | 4 | 4 | 3 |
Interview Context
Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.
PostgreSQL 15
Ready to execute
Click "Run Query" or press Ctrl + Enter to test your query.
Output will be verified against the test dataset.