Back to Library

ROW_NUMBER() in SQL

Assigns a unique sequential integer to rows within a result set partition.

Syntax

ROW_NUMBER() OVER (
    [PARTITION BY column1, column2, ...]
    ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...
)
Return type
BIGINT

ROW_NUMBER() Function Example

SELECT ROW_NUMBER() OVER (ORDER BY sales_amount DESC) AS rank, 
       product_name, sales_amount 
FROM sales;
-- Output: 1, 'Product A', 1000

What is ROW_NUMBER() in SQL?

The ROW_NUMBER() function in SQL assigns a unique sequential integer to each row within a result set partition, starting at 1 for each partition. It is commonly used for ranking results, paginating query outputs, and generating row-based identifiers dynamically.

This function is supported in SQL Server, PostgreSQL, and Oracle, while MySQL uses window functions with ROW_NUMBER() introduced in version 8.0.

The ROW_NUMBER() function is widely used in pagination, duplicate handling, and organizing ranked datasets.

Parameters:

  • PARTITION BY (optional): Columns to partition the rows

  • ORDER BY (required): Columns that determine the row sequence

Example Use Cases:

-- Top N per group

SELECT * FROM (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY category ORDER BY sales DESC) AS rank
    FROM sales
) ranked WHERE rank <= 3;
-- Output:
category    product_id    sales    rank
--------    ----------    -----    ----
Books       P103          1500     1
Books       P101          1200     2
Books       P105          950      3
Electronics E201          3500     1
Electronics E204          3200     2
Electronics E202          2800     3
Clothing    C301          890      1
Clothing    C304          780      2
Clothing    C302          650      3

-- Find duplicate records

SELECT * FROM (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY email ORDER BY id) AS rn
    FROM customers
) duplicates WHERE rn > 1;
-- Output:
id    email               name           rn
--    ----------------    -----------    --
102   john@email.com     John Smith     2
105   sarah@email.com    Sarah Jones    2
108   mike@email.com     Mike Wilson    2
110   john@email.com     Johnny Smith   3

-- Pagination

SELECT * FROM (
    SELECT *, ROW_NUMBER() OVER (ORDER BY date_created DESC) AS row_num
    FROM posts
) numbered WHERE row_num BETWEEN 1 AND 10;
-- Output:
post_id    title              date_created    row_num
-------    ---------------    ------------    -------
P001       Latest Update      2024-12-28     1
P002       Tech News          2024-12-27     2
P003       Product Launch     2024-12-26     3
P004       Company Update     2024-12-25     4
P005       Year in Review     2024-12-24     5
P006       Holiday Special    2024-12-23     6
P007       Market Analysis    2024-12-22     7
P008       Industry Trends    2024-12-21     8
P009       Success Story      2024-12-20     9
P010       Team Spotlight     2024-12-19     10

Notes:

  • Behavior: Always returns unique, sequential numbers

  • Performance Considerations: May require window function optimization

  • Version Info: Core window function available in most modern DBMS

  • Deprecated/Recommended Alternatives: None, standard functionality

Error Handling:

  • Error: Syntax error if ORDER BY is omitted

  • Recommendation: Always include ORDER BY clause


Supported Databases:

DBMS
Function / Syntax
Example
Behavior with NULL
MySQL

ROW_NUMBER()

ROW_NUMBER() OVER (ORDER BY col)

Includes NULL values

PostgreSQL

ROW_NUMBER()

ROW_NUMBER() OVER (ORDER BY col)

Includes NULL values

SQL Server

ROW_NUMBER()

ROW_NUMBER() OVER (ORDER BY col)

Includes NULL values

SQLite

ROW_NUMBER()

ROW_NUMBER() OVER (ORDER BY col)

Includes NULL values

BigQuery

ROW_NUMBER()

ROW_NUMBER() OVER (ORDER BY col)

Includes NULL values

Snowflake

ROW_NUMBER()

ROW_NUMBER() OVER (ORDER BY col)

Includes NULL values

Athena

ROW_NUMBER()

ROW_NUMBER() OVER (ORDER BY col)

Includes NULL values

Related Functions:

CAST/CONVERT()

Converts a value from one data type to another. Both CAST and CONVERT perform the same function with different syntax.

Converts a value from one data type to another. Both CAST and CONVERT perform the same function with different syntax.

Converts a value from one data type to another. Both CAST and CONVERT perform the same function with different syntax.

Converts a value from one data type to another. Both CAST and CONVERT perform the same function with different syntax.

COALESCE()

Returns the first non-null value in a list of expressions, evaluated from left to right.

Returns the first non-null value in a list of expressions, evaluated from left to right.

Returns the first non-null value in a list of expressions, evaluated from left to right.

Returns the first non-null value in a list of expressions, evaluated from left to right.

LAST_VALUE()

Returns the last value in an ordered partition of a result set.

Returns the last value in an ordered partition of a result set.

Returns the last value in an ordered partition of a result set.

Returns the last value in an ordered partition of a result set.

FIRST_VALUE()

Returns the first value in an ordered partition of a result set.

Returns the first value in an ordered partition of a result set.

Returns the first value in an ordered partition of a result set.

Returns the first value in an ordered partition of a result set.

CASE

Provides conditional logic in SQL queries, evaluating conditions and returning specified results based on matching conditions.

Provides conditional logic in SQL queries, evaluating conditions and returning specified results based on matching conditions.

Provides conditional logic in SQL queries, evaluating conditions and returning specified results based on matching conditions.

Provides conditional logic in SQL queries, evaluating conditions and returning specified results based on matching conditions.

ROW_NUMBER()

Assigns a unique sequential integer to rows within a result set partition.

Assigns a unique sequential integer to rows within a result set partition.

Assigns a unique sequential integer to rows within a result set partition.

Assigns a unique sequential integer to rows within a result set partition.

One place for all your queries,
directly on your SQL editor