Back to Library

RANK() in SQL

Assigns a rank to rows within a result set partition, with gaps in rank numbers when ties occur.

Syntax

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

RANK() Function Example

SELECT RANK() OVER (ORDER BY sales_amount DESC) AS rank, 
       product_name, sales_amount 
FROM sales;
-- Output: 1, 'Product A', 1000 
--         1, 'Product B', 1000
--         3, 'Product C', 800

What is RANK() in SQL?

The RANK() function in SQL assigns a rank to each row within a result set partition, with gaps in rank numbers when ties occur. Unlike ROW_NUMBER(), which always assigns unique sequential numbers, RANK() gives the same rank to rows with identical values, leaving gaps in the ranking sequence.

This function is commonly used for ranking results in leaderboards, performance metrics, and statistical analysis. It is supported in SQL Server, PostgreSQL, and Oracle, and available in MySQL 8.0+ as a window function.

RANK() is widely used in competition ranking, sales leaderboards, and percentile-based reporting.

Parameters:

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

  • ORDER BY (required): Columns that determine the ranking order

Example Use Cases:

-- Top performers by department

SELECT * FROM (
    SELECT *, RANK() OVER (PARTITION BY department ORDER BY performance DESC) AS rank
    FROM employees
) ranked WHERE rank <= 3;
-- Output:
department    employee_name    performance    rank
----------    -------------    -----------    ----
Sales         John Smith       95             1
Sales         Sarah Jones      95             1
Sales         Mike Wilson      92             3
Marketing     Lisa Brown       98             1
Marketing     David Lee       94             2
Marketing     Amy Chen        90             3
Engineering   Bob Miller      97             1
Engineering   Jane Doe        96             2
Engineering   Tom Clark       94             3

-- Product ranking by sales

SELECT product_name,
       sales_amount,
       RANK() OVER (ORDER BY sales_amount DESC) AS sales_rank
FROM products;
-- Output:
product_name        sales_amount    sales_rank
--------------      ------------    ----------
iPhone 13 Pro       150000         1
Samsung Galaxy S21  145000         2
MacBook Pro         140000         2
iPad Air            125000         4
AirPods Pro         100000         5

-- Student grades ranking

SELECT student_name,
       score,
       RANK() OVER (ORDER BY score DESC) AS grade_rank
FROM exam_results;
-- Output:
student_name    score    grade_rank
------------    -----    ----------
Alice Johnson   98       1
Bob Smith       98       1
Carol White     95       3
David Brown     92       4
Emma Davis      90       5

Notes:

  • Behavior: Equal values receive same rank, next rank skips

  • Performance Considerations: May require window function optimization

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

  • Deprecated/Recommended Alternatives: Consider DENSE_RANK() if gaps undesired

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

RANK()

RANK() OVER (ORDER BY col)

NULLs ranked together

PostgreSQL

RANK()

RANK() OVER (ORDER BY col)

NULLs ranked last by default

SQL Server

RANK()

RANK() OVER (ORDER BY col)

NULLs ranked last by default

SQLite

RANK()

RANK() OVER (ORDER BY col)

NULLs ranked together

BigQuery

RANK()

RANK() OVER (ORDER BY col)

NULLs ranked together

Snowflake

RANK()

RANK() OVER (ORDER BY col)

NULLs ranked last by default

Athena

RANK()

RANK() OVER (ORDER BY col)

NULLs ranked last by default

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.

LEAD()

Accesses data from a subsequent row in the result set within a specified window partition.

Accesses data from a subsequent row in the result set within a specified window partition.

Accesses data from a subsequent row in the result set within a specified window partition.

Accesses data from a subsequent row in the result set within a specified window partition.

LAG()

Accesses data from a previous row in the result set within a specified window partition.

Accesses data from a previous row in the result set within a specified window partition.

Accesses data from a previous row in the result set within a specified window partition.

Accesses data from a previous row in the result set within a specified window partition.

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.

RANK()

Assigns a rank to rows within a result set partition, with gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with gaps in rank numbers when ties occur.

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