What is the difference between ROW_NUMBER, RANK and DENSE_RANK?
Scenario
HR wants to rank employees by salary within each department. Ben and Chen in Engineering earn exactly the same, and the ranking has to treat them fairly.
Interview answerTry answering first, then reveal
All three number rows within a window, and they differ only on ties. ROW_NUMBER is always unique (1, 2, 3, 4). RANK gives tied rows the same number and then skips (1, 2, 2, 4). DENSE_RANK gives tied rows the same number with no gap (1, 2, 2, 3). Pick the one whose tie behaviour matches the business rule.
Explanation
A window function computes a value for each row from a set of related rows (the window) without collapsing them the way GROUP BY does. PARTITION BY restarts the numbering for each group and ORDER BY inside OVER decides the order of numbering.
With no ties, the three functions return identical numbers. With ties, ROW_NUMBER still has to put one tied row first, and which one it picks is arbitrary unless the ORDER BY includes a unique tie-breaker. That is why the example below orders ROW_NUMBER by salary and then name.
RANK behaves like a sports table: two people sharing 2nd place means nobody is 3rd. DENSE_RANK counts distinct values, so it is the natural choice for "the Nth highest salary".
Worked example
Run on DuckDB when this page was built; the results shown are the real output.
Sample data
CREATE TABLE employees (emp_id INT, name VARCHAR, dept VARCHAR, salary INT);
INSERT INTO employees VALUES
(1, 'Asha', 'Engineering', 120), (2, 'Ben', 'Engineering', 110),
(3, 'Chen', 'Engineering', 110), (4, 'Dev', 'Engineering', 90),
(5, 'Eli', 'Sales', 95), (6, 'Fay', 'Sales', 80);All three side by side
SELECT dept, name, salary,
ROW_NUMBER() OVER (PARTITION BY dept ORDER BY salary DESC, name) AS row_num,
RANK() OVER (PARTITION BY dept ORDER BY salary DESC) AS rnk,
DENSE_RANK() OVER (PARTITION BY dept ORDER BY salary DESC) AS dense_rnk
FROM employees
ORDER BY dept, salary DESC, name;| dept | name | salary | row_num | rnk | dense_rnk |
|---|---|---|---|---|---|
| Engineering | Asha | 120 | 1 | 1 | 1 |
| Engineering | Ben | 110 | 2 | 2 | 2 |
| Engineering | Chen | 110 | 3 | 2 | 2 |
| Engineering | Dev | 90 | 4 | 4 | 3 |
| Sales | Eli | 95 | 1 | 1 | 1 |
| Sales | Fay | 80 | 2 | 2 | 2 |
Ben and Chen share rank 2. After them, Dev is 4 under RANK but 3 under DENSE_RANK.
In production
- Always add a unique tie-breaker (a key or timestamp) when
ROW_NUMBERdrives deduplication or pagination. Without one, a rerun can keep a different row and results drift between runs. - In Spark and Databricks a window with
PARTITION BYshuffles data by the partition key and sorts each partition. One huge partition (a skewed key) becomes one slow task. - A window with no
PARTITION BYsends every row to a single partition. On large tables, aggregate first or partition by something.
Common mistakes
- Using
ROW_NUMBERfor "top 3 salaries" and silently dropping an employee tied for 3rd. - Using
RANKfor "the 3rd highest salary": with a tie for 2nd there is no rank 3, so the query returns nothing. - Forgetting
PARTITION BY, which ranks across the whole table instead of per department.
Related questions
More on this topic in the SQL Interview Questions guide, or browse all SQL questions.