How to Rank Rows in Oracle

Last updated August 25, 2026 · By the SaturnSQL team

RANK, DENSE_RANK, and ROW_NUMBER all number rows over a window but differ on ties: RANK leaves gaps, DENSE_RANK does not, ROW_NUMBER ignores ties.

SELECT employee_id, salary,
       RANK()       OVER (ORDER BY salary DESC) AS rnk,
       DENSE_RANK() OVER (ORDER BY salary DESC) AS dense_rnk,
       ROW_NUMBER() OVER (ORDER BY salary DESC) AS row_num
FROM employees;

How ties are handled

If two employees tie for the highest salary, RANK gives both a 1 and then jumps the next row straight to 3, skipping 2. DENSE_RANK also gives both a 1 but the next distinct salary gets 2, with no gap. ROW_NUMBER hands out 1 and 2 to the tied pair based on an arbitrary tiebreak, which is why it needs a fully deterministic ORDER BY to be reproducible.

Ranking within groups

SELECT department_id, employee_id, salary,
       RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS dept_rank
FROM employees;

Pick RANK when you want 'joint 1st place' style reporting, DENSE_RANK when downstream code expects a contiguous 1..N scale (like a compact tier number), and ROW_NUMBER when you specifically need exactly one row per position.

Run this in SaturnSQL

SaturnSQL is a browser-based SQL editor for teams: shared query library, schema-aware autocomplete, and scheduled exports to Google Sheets and Slack.

Try it free

Do more with Oracle

Related Oracle guides