Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 10.0.1 » SQL Anywhere Server - SQL Usage » OLAP Support » Window functions » Window ranking functions

Window ranking functions Next Page

RANK function


You use the RANK function to return the rank of the value in the current row as compared to the value in other rows. The rank of a value reflects the order in which it would appear if the list of values was sorted.

When using the RANK function, the rank is calculated for the expression specified in the window's ORDER BY clause. If the ORDER BY clause includes multiple expressions, the second and subsequent expressions are used to break ties if the first expression has the same value in adjacent rows. NULL values are sorted before any other value (in ascending sequence).

Example 1

The following query determines the three most expensive products in the database. A descending sort sequence is specified for the window so that the most expensive products have the lowest rank, that is, rankings start at 1.

SELECT Top 3 *
       FROM ( SELECT Description, Quantity, UnitPrice,
              RANK() OVER ( ORDER BY UnitPrice DESC ) AS Rank 
              FROM Products ) AS DT
ORDER BY Rank;

This query returns the following result:

DescriptionQuantityUnitPriceRank
1Zipped Sweatshirt3224.001
2Hooded Sweatshirt3924.001
3Cotton Shorts8015.003

Note that rows 1 and 2 have the same value for Unit Price, and therefore also have the same rank. This is called a tie.

With the RANK function, the rank value jumps after a tie. For example, the rank value for row 3 has jumped to three instead of 2. This is different from the DENSE_RANK function, where no jumping occurs after a tie. See DENSE_RANK function.

Example 2

The following SQL query finds the male and female employees from Utah and ranks them in descending order according to salary.

SELECT Surname, Salary, Sex,
     RANK() OVER ( ORDER BY Salary DESC ) "Rank"
     FROM Employees 
WHERE State IN ( 'UT' );

The table that follows represents the result set from the query:

SurnameSalarySexRank
1Shishov72995.00F1
2Wang68400.00M2
3Cobb62000.00M3
4Morris61300.00M4
5Diaz54900.00M5
6Driscoll48023.69M6
7Hildebrand45829.00F7
8Goggin37900.00M8
9Rebeiro34576.00M9
10Bigelow31200.00F10
11Lynch24903.00M11
Example 3

You can partition your data to provide different results. Using the query from Example 2, you can change the data by partitioning it by gender. The following example ranks employees in descending order by salary and partitions by gender.

SELECT Surname, Salary, Sex,
     RANK () OVER ( PARTITION BY Sex
     ORDER BY Salary DESC ) "Rank"
     FROM Employees 
WHERE State IN ( 'UT' );

The table that follows represents the result set from the query:

SurnameSalarySexRank
1Wang68400.00M1
2Cobb62000.00M2
3Morris61300.00M3
4Diaz54900.00M4
5Driscoll48023.69M5
6Goggin37900.00M6
7Rebeiro34576.00M7
8Lynch24903.00M8
9Shishov72995.00F1
10Hildebrand45829.00F2
11Bigelow31200.00F3

For more information on the syntax for the RANK function, see RANK function [Ranking].