RANK
Overview
The RANK()
window function determines the rank of a value in a group of values, based on the ORDER BY
expression
in the OVER
clause. It can be used with all data types supported by Oxla.
Syntax
The syntax for this function is as follows:
Rows with equal values for the ranking criteria receive the same rank. The output type for this function is BIGINT
and it indicates the rank of values in a table, regardles of the input types.
- If the optional
PARTITION BY
expression is present, the rankings are reset for each group of rows - If the
ORDER BY
expression is omitted then all ranks are equal to 1
Parameters
()
: this function takes no arguments but parentheses is required
Examples
For the needs of this section, we will create the winsales
table that stores details about some sales transactions:
RANK() with ORDER BY
In this example, we will focus on executing the RANK()
function with ORDER BY
keyword and calculate the rank of all rows based on the quantity sold:
Here is the output for the query presented above that includes the sales ID along with the quantity sold and regular ranks:
RANK() with ORDER BY and PARTITION BY
In this example, we will focus on executing the RANK()
function with ORDER BY
keyword and PARTITION BY
clause, partition the table by seller ID, order each partition by the quantity and assign a rank to each row:
Here is the output for the query presented above: