Section: Ranking functions

ROW_NUMBER

sql
sql
SELECT employee_id,
       department_id,
       salary,
       ROW_NUMBER() OVER (
         PARTITION BY department_id
         ORDER BY salary DESC
       ) AS rn
FROM employees;
Explanation

`ROW_NUMBER` always assigns distinct numbers, even when sort values tie.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Ranking functions
RANK
Tied rows share rank and leave gaps.
OpenIn sheetsqlsame section
DENSE_RANK
Tied rows share rank without gaps.
OpenIn sheetsqlsame section
Split rows into quartiles with NTILE
Bucket ordered rows into four groups.
OpenIn sheetsqlsame section
Calculate percent rank
Relative position from 0 to 1.
OpenIn sheetsqlsame section
Cumulative distribution
Fraction of rows at or below the current row.
OpenIn sheetsqlsame section
Get the latest row per group
Use ROW_NUMBER in a subquery or CTE.