SQLSQL
#41

ROW_NUMBER vs RANK vs DENSE_RANK in SQL Explained

MediumAmazon
Interview Question #41
Asked at Amazon

Given 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_namesalary
Alice10000
Bob9000
Charlie9000
Dave8000
Expected Output Structure4 rows
emp_namesalaryrow_numrank_valdense_rank_val
Alice10000111
Bob9000222
Charlie9000322
Dave8000443
Interview Context

Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.

SQLPostgreSQL 15
Ready to execute

Click "Run Query" or press Ctrl + Enter to test your query.

Output will be verified against the test dataset.

Chat with us