← Back to SQL, page 6

sql-0114 · Easy fresher

Given salaries 120000, 95000, 95000, 70000, what do ROW_NUMBER, RANK and DENSE_RANK return?

ROW_NUMBER gives 1, 2, 3, 4: always unique, with ties broken arbitrarily unless ORDER BY decides. RANK gives 1, 2, 2, 4: ties share a rank and the next rank skips. DENSE_RANK gives 1, 2, 2, 3: ties share a rank and no numbers are skipped.

Full answer

All three number rows in the order given by OVER (ORDER BY ...). They differ only in how they treat ties.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');
sql
SELECT name, salary,
       ROW_NUMBER() OVER (ORDER BY salary DESC, name) AS row_num,
       RANK()       OVER (ORDER BY salary DESC)       AS rnk,
       DENSE_RANK() OVER (ORDER BY salary DESC)       AS dense_rnk
FROM employees
WHERE salary >= 70000
ORDER BY salary DESC, name;
namesalaryrow_numrnkdense_rnk
Asha120000111
Mei95000222
Ravi95000322
Tom70000443
  • ROW_NUMBER never repeats. Ravi and Mei earn the same, so the name tie-breaker in the ORDER BY decides who gets 2 and who gets 3. Without a tie-breaker, the assignment between tied rows is arbitrary.
  • RANK gives tied rows the same rank and then leaves a gap, like a race result where two runners share second place and the next runner is fourth.
  • DENSE_RANK gives tied rows the same rank and continues with the next number, so there are no gaps.

Which to use depends on the question. "Exactly one row per group" needs ROW_NUMBER. "Top 3 salaries, including everyone tied" needs DENSE_RANK with <= 3. "Olympic-style position" needs RANK.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

SELECT name, salary,
       ROW_NUMBER() OVER (ORDER BY salary DESC, name) AS row_num,
       RANK()       OVER (ORDER BY salary DESC)       AS rnk,
       DENSE_RANK() OVER (ORDER BY salary DESC)       AS dense_rnk
FROM employees
WHERE salary >= 70000
ORDER BY salary DESC, name;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • ROW_NUMBER: unique numbers, ties broken by ORDER BY or arbitrarily
  • RANK: ties share a rank, then a gap
  • DENSE_RANK: ties share a rank, no gaps
  • Pick by question: one per group, distinct salary levels, or positions

Common mistakes

  • Using ROW_NUMBER for 'top N salaries' and silently dropping tied employees
  • Leaving ROW_NUMBER without a tie-breaker and getting different results across runs

Follow-ups

  • How would you find the second-highest distinct salary?
  • What does NTILE(4) do?

Tips

  • Use a race result to explain RANK's gap