DENSE_RANK
Overview
The DENSE_RANK()
window function assigns a rank for each value within a specified group, 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:
The output type for this function is a BIGINT
and it indicates the rank of values in a table, regardless of the input types. If the ORDER BY
expression is omitted, all ranks will default to 1. In case an optional PARTITION BY
expression is included, the rankings are reset for each group of rows. The rows with equal values for the ranking criteria receive the same rank.
RANK()
function, there is no gap in the sequence of ranked values (if two rows are ranked 1, the next rank will be 2)Parameters
()
: the function takes no parameters, but empty parentheses is required
Examples
For the needs of this section, we will create a winsales
table that stores information about some sales transactions:
DENSE_RANK() with ORDER BY
In this example we will focus on executing the DENSE_RANK()
function with ORDER BY
keyword and calculate the descending dense 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 both dense and regular ranks:
DENSE_RANK() with ORDER BY and PARTITION_BY
In this example we will focus on executing the DENSE_RANK()
function with ORDER BY
keyword and PARTITION BY
clause and partition the table by seller ID, then order each partition by the quantity and assign a dense rank to each row:
Here is the output for the query presented above: