SQL Flow Field Guide

ROW_NUMBER

Catalog support for SQL Server 2022 (16.x) and 2025 (17.x)

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 Flow
SELECT 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.