Skip to content

RANK

RANK() OVER ( [PARTITION BY partition_expression] ORDER BY sort_expression [ASC | DESC])

Section titled “RANK() OVER ( [PARTITION BY partition_expression] ORDER BY sort_expression [ASC | DESC])”
SELECT
product,
price,
RANK() OVER (
ORDER BY price DESC
) AS PriceRank
FROM
Products;
| product | price | PriceRank |
|---------|-------|-----------|
| ProductD| 800 | 1 |
| ProductA| 600 | 2 |
| ProductC| 550 | 3 |
| ProductB| 450 | 4 |

The example code ranks each row of the ‘Products’ table by the ‘price’ column in descending order; this is done with the RANK() function. The function arranges the prices from the highest to the lowest. The rank of each row is displayed in the ‘PriceRank’ column.