Skip to main content
Rank the current row within the requested window with gaps. For more information on usage, please refer to Window Functions.

Syntax

RANK() OVER ([PARTITION BY <partition_by>] ORDER BY <order_by> [ASC|DESC] )

Parameters

ParameterDescriptionSupported input types
<partition_by>The expression used for the PARTITION BY clause.Any
<order_by>The expression used in the ORDER BY clause. This parameter determines what value will be ranked.Any

Return Type

INTEGER

Example

In this example, players are ranked by their score within each game level. Players with equal scores receive the same rank, and the next rank is skipped.
SELECT
    nickname,
    level,
    current_score,
    RANK() OVER (PARTITION BY level ORDER BY current_score DESC) AS rank_in_game
FROM
    (VALUES
        ('kennethpark', 9, 76),
        ('burchdenise', 9, 89),
        ('ymatthews', 9, 75),
        ('sabrina21', 9, 89)
    ) AS t(nickname, level, current_score)
ORDER BY level, nickname;
nickname textlevel intcurrent_score intrank_in_game long
burchdenise9891
kennethpark9763
sabrina219891
ymatthews9754

Rows: 4Execution time: 2.79ms