Advanced · Lesson 2 of 18

RANK and DENSE_RANK

RANK and DENSE_RANK handle ties differently than ROW_NUMBER.

Given salaries: 100, 100, 90, 80

ROW_NUMBER(): 1, 2, 3, 4 (ignores ties) RANK(): 1, 1, 3, 4 (skips after ties) DENSE_RANK(): 1, 1, 2, 3 (no gaps after ties)

Example:

SELECT first_name, salary,
       RANK() OVER (ORDER BY salary DESC) as rnk
FROM employees;

Use RANK for competition-style rankings. Use DENSE_RANK when you don't want gaps.

Check your understanding

Rank employees by salary using DENSE_RANK (no gaps for ties).

Examples run on the employees sample database. Open any of them in the playground to experiment.