RANK
functionsupportedCore SQL functionsReturns the rank of a value within an ordered group of values. The rank value starts at 1 and continues up sequentially. If two values are the same, they have the same rank.
Syntax
supportedpartialnot supportednot scored
RANK() OVER ( [ PARTITION BY <expr1> ]
ORDER BY <expr2> [ { ASC | DESC } ] [ NULLS { FIRST | LAST } ] [ <window_frame> ] )
Usage notes
- expr1 The column or expression to partition the window by. For example, suppose that within each state or province, you want to rank farmers in order by the amount of corn they produced. In this case, you partition by state. If you want only a single group (e.g. you want to rank all farmers in the U.S. regardless of which state they live in), then omit the PARTITION BY clause.
- expr2 The column or expression to order (rank) by. For example, if you’re ranking farmers to see who produced the most corn (within their state), then you would use the bushels_produced column. For details, see Examples (in this topic).
- Tie values result in the same rank value; however, gaps in the sequence result from the tie values. For example, if the first three rows return 1, RANK skips 2 and 3 and assigns 4 to the next row in the group.
- To avoid gaps, use the DENSE_RANK function instead.