ROW_NUMBER
ranking
What changes
- Input
- partition and ORDER BY columns of OVER
- Output or change
- one rank or number per row
- Return type
- bigint
Syntax at a glance
ROW_NUMBER ( ) OVER ( [ PARTITION BY value_expression , ... [ n ] ] order_by_clause )
Work example
Number rows for stable paging or keep one chosen row per group.
Add a unique tie-breaker to ORDER BY when the row number must be repeatable.
Check your work ROW_NUMBER starts again in each partition and is not stable when its order has ties.
Practice SQL
Open in SQL FlowSELECT n, ROW_NUMBER() OVER (ORDER BY n) AS row_no FROM (VALUES (30), (10), (20)) AS v(n);
Open Microsoft Learn for full syntax, permissions, and details for your version.