ORACLE Rank and Dense_rank functions

ORACLE Rank and Dense_rank functions – Learn how to calculate nth maximum and minimum value in the result set  based on the ranking mechanism either in ascending order or descending order using Rank and Dens_Rank functions.

Below is the table named STD to perform Ranking operations.

STDNONAMEJOINDATEFEEDEPTNO
7369SMITH17-DEC-80 12.00.00 AM80001
7499ALLEN20-FEB-81 12.00.00 AM160003
7521WARD22-FEB-81 12.00.00 AM125003
7566JONES02-APR-81 12.00.00 AM29752
7654MARTIN28-SEP-81 12.00.00 AM125003
7698BLAKE01-MAY-81 12.00.00 AM285003
7844TURNER08-SEP-81 12.00.00 AM15003

ORACLE Rank and Dense_rank functions

RANK

Rank function returns the rank of each row or rank of values in a group of values in result set.
Rank function gives ranks in non consecutive order if row contains same values.
Rank assigns the same rank to identical values.

For Example consider that there are same names in the table as shown below.

NAME
SMITH
ALLEN
SMITH
ALLEN
WARD

Rank returns the 1,1,3,3,5 as result for above example based on the name column.

NAMERANK
ALLEN1
ALLEN1
SMITH3
SMITH3
WARD5

RANK() FUNCTION SYNTAX

Rank() OVER ([PARTITION BY [COLUMN_NMAE]] ORDER BY [COLUMN_NMAE] [ASC] OR [DESC])

Here is the example to give the rank for highest fee payers in the STD table.

SELECT STDNO,SNAME,FEE,RANK() OVER(ORDER BY FEE DESC)AS RANK FROM STD;

NOTE

RANK Function will assign same number as rank if there exists a same value and will skip those many ranks when giving rank to another value.

Output

STDNONAMEFEERANK
7698BLAKE285001
7499ALLEN160002
7521WARD125003
7654MARTIN125003
7369SMITH80005
7566JONES29756
7844TURNER15007

Rank function with PARTITION BY

here is the example to give the rank for highest fee payer based on department wise.

SELECT STDNO,SNAME,FEE,deptno,RANK() OVER( partition by deptno ORDER BY FEE DESC)AS RANK FROM STD;
STDNONAMEFEEDEPTNORANK
7369SMITH800011
7566JONES297521
7698BLAKE2850031
7499ALLEN1600032
7521WARD1250033
7654MARTIN1250033
7844TURNER150035

DENSE RANK()

Dense Rank function returns the rank of each row or rank of values in a group of values in result set.
Dense Rank function gives ranks in consecutive order without gaps in the ranking.

Rank assigns the same rank to identical values.

For Example consider that there are same names in the table as shown below.

NAME
SMITH
ALLEN
SMITH
ALLEN
WARD

Rank returns the 1,1,2,2,3 as result for above example based on the name column.

NAMERANK
ALLEN1
ALLEN1
SMITH2
SMITH2
WARD3

DENSE RANK() FUNCTION SYNTAX

DENSE_RANK() OVER ([PARTITION BY [COLUMN_NMAE]] ORDER BY [COLUMN_NMAE] [ASC] OR [DESC])

Here is the example to give the rank for highest fee payers in the STD table.

SELECT STDNO,SNAME,FEE,dense_RANK() OVER(ORDER BY FEE DESC)AS RANK FROM STD;

Output

STDNONAMEFEERANK
7698BLAKE285001
7499ALLEN160002
7521WARD125003
7654MARTIN125003
7369SMITH80004
7566JONES29755
7844TURNER15006

NOTE

Dense_RANK Function will assign same number as rank if there exists a same value and will give rank in consecutive order without gaps in the ranking.

DENSE_Rank function with PARTITION BY

here is the example to give the rank for highest fee payer based on department wise.

SELECT STDNO,SNAME,FEE,DEPTNO,dense_RANK() OVER( partition by DEPTNO ORDER BY FEE DESC)AS RANK FROM STD;
STDNONAMEFEEDEPTNORANK
7369SMITH800011
7566JONES297521
7698BLAKE2850031
7499ALLEN1600032
7521WARD1250033
7654MARTIN1250033
7844TURNER150034

Related Posts