This Oracle tutorial explains how to use the Oracle/PLSQL RANK function with syntax and examples.
The Oracle/PLSQL RANK function returns the rank of a value in a group of values. It is very similar to the DENSE_RANK function. However, the rank function can cause non-consecutive rankings if the tested values are the same. Whereas, the DENSE_RANK function will always result in consecutive rankings.
The RANK function can be used two ways - as an Aggregate function or as an Analytic function.
As an Aggregate function, the RANK function returns the rank of a row within a group of rows.
The syntax for the RANK function when used as an Aggregate function is:
First expression which identifies a unique row in the group.
Optional. Additional expressions which identifies a unique row in the group.
The RANK function returns a numeric value.
The RANK function can be used in the following versions of Oracle/PLSQL:
Let's look at some Oracle RANK function examples and explore how to use the RANK function in Oracle/PLSQL.
For example:
The SQL statement above would return the rank of an employee with a salary of $1,000 and a bonus of $500 from within the employees table.
As an Analytic function, the RANK function returns the rank of each row of a query with respective to the other rows.
The syntax for the RANK function when used as an Analytic function is:
The RANK function returns a numeric value.
The RANK function can be used in the following versions of Oracle/PLSQL:
The SQL statement above would return all employees who work in the Marketing department and then calculate a rank for each unique salary in the Marketing department. If two employees had the same salary, the RANK function would return the same rank for both employees. However, this will cause a gap in the ranks (ie: non-consecutive ranks). This is quite different from the DENSE_RANK function which generates consecutive rankings.