0% found this document useful (0 votes)
83 views5 pages

7.1 Ranking Functions

The document discusses different ranking functions in SQL: ROW_NUMBER(), RANK(), and DENSE_RANK(). ROW_NUMBER() assigns a unique, sequential number to each row. RANK() assigns the same rank to ties, and leaves gaps in the ranking numbers. DENSE_RANK() assigns the same rank to ties, without gaps in the numbers. The ranking functions are used with the OVER clause to define partitions within a result set and sort order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views5 pages

7.1 Ranking Functions

The document discusses different ranking functions in SQL: ROW_NUMBER(), RANK(), and DENSE_RANK(). ROW_NUMBER() assigns a unique, sequential number to each row. RANK() assigns the same rank to ties, and leaves gaps in the ranking numbers. DENSE_RANK() assigns the same rank to ties, without gaps in the numbers. The ranking functions are used with the OVER clause to define partitions within a result set and sort order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

RANKING FUNCTIONS: These functions returns rank for each and every row in the result set.

SQL supports four kinds of ranking function:

1. ROW_NUMBER()
2. RANK()
3. DENSE_RANK()

Syn: Ranking_function OVER(Partition by ColumnName Order by ColumnName asc/desc)

NOTE: In the above syntax Partition by clause is optional and over() clause and order by clauses are
mandatory.

By default order by clause keeps the data in ascending order.

ROW_NUMBER(): This function returns serial number to every row irrespective of duplicates values.

Eg: SELECT ENAME,DEPTNO,SAL,ROW_NUMBER() OVER(ORDER BY SAL) ROW_NUM FROM EMP


Output:

ENAME DEPTNO SAL ROW_NUM


SMITH 20 800.00 1
JAMES 30 950.00 2
ADAMS 20 1100.00 3
WARD 30 1250.00 4
MARTIN 30 1250.00 5
ALLEN 30 1300.00 6
TURNER 30 1300.00 7
MILLER 10 1300.00 8
CLARK 10 2450.00 9
BLAKE 30 2850.00 10
JONES 20 2975.00 11
SCOTT 20 3000.00 12
FORD 20 3000.00 13
KING 10 5000.00 14

Here FROM clause takes all data (all columns and all rows) from EMP table and ORDER BY clause sorts
the data in ascending order based on SAL column and ROW_NUMBER() function gives serial numbers to
each and every row and this serial number column we named as ROW_NUM and finally SELECT displays
ENAME DEPTNO and SAL along with new column ROW_NUM.

Execution Steps:
FROM
ORDER BY
ROW_NUMBER()
SELECT

Eg: SELECT ENAME,DEPTNO,SAL,ROW_NUMBER() OVER(PARTITION BY DEPTNO ORDER BY SAL)


ROW_NUM FROM EMP

ENAME DEPTNO SAL ROW_NUM


MILLER 10 1300.00 1
CLARK 10 2450.00 2
KING 10 5000.00 3
SMITH 20 800.00 1
ADAMS 20 1100.00 2
JONES 20 2975.00 3
SCOTT 20 3000.00 4
FORD 20 3000.00 5
JAMES 30 950.00 1
WARD 30 1250.00 2
MARTIN 30 1250.00 3
ALLEN 30 1300.00 4
TURNER 30 1300.00 5
BLAKE 30 2850.00 6

Here FROM clause takes all data(all columns and all rows) from EMP table and PARTITION BY clause
divides the table into small partitions based on the DEPTNOs and ORDER BY clause sorts the data of each
partitions, that means each partition starts sorting order from 1. ROW_NUMBER() function gives serial
numbers to each partition. Finally SELECT displays ENAME, DEPTNO,SAL and ROW_NUM columns

Execution Order:

FROM
PARTITION BY
ORDER BY
ROW_NUMBER()
SELECT

RANK(): This function gives same rank for duplicate values and also gives gaps between ranks.

Eg: SELECT ENAME,DEPTNO,SAL,RANK() OVER(ORDER BY SAL) RANK_FUN FROM EMP


OutPut:

ENAME DEPTNO SAL RANK_NUM


SMITH 20 800.00 1
JAMES 30 950.00 2
ADAMS 20 1100.00 3
WARD 30 1250.00 4
MARTIN 30 1250.00 4
ALLEN 30 1300.00 6
TURNER 30 1300.00 6
MILLER 10 1300.00 6
CLARK 10 2450.00 9
BLAKE 30 2850.00 10
JONES 20 2975.00 11
SCOTT 20 3000.00 12
FORD 20 3000.00 12
KING 10 5000.00 14

Here FROM clause takes all data(all columns and all rows) from EMP table. Order by clause keeps the
data in sorting order and RANK() gives numbers to the every row but it gives same number for duplicate
values and also gives gaps

Exectio Order:

FROM
ORDER BY
RANK()
SELECT

Eg: SELECT ENAME,DEPTNO,SAL,RANK() OVER(PARTITION BY DEPTNO ORDER BY SAL) RANK_FUN FROM


EMP
OUTPUT:

ENAME DEPTNO SAL RANK_FUN


MILLER 10 1300.00 1
CLARK 10 2450.00 2
KING 10 5000.00 3
SMITH 20 800.00 1
ADAMS 20 1100.00 2
JONES 20 2975.00 3
SCOTT 20 3000.00 4
FORD 20 3000.00 4
JAMES 30 950.00 1
WARD 30 1250.00 2
MARTIN 30 1250.00 2
ALLEN 30 1300.00 4
TURNER 30 1300.00 4
BLAKE 30 2850.00 6
Here FROM clause takes all data(all columns and all rows) from EMP table. Partition by clause divides
the table into small partitions based on the DEPTNOs and ORDER BY clause keeps the data of each
partition in sorting order and RANK() gives numbers to each value in the each partition. RANK() gives
same number to duplicate values and also gives gaps.

Execution Order:

FROM
PARTITION BY
ORDER BY
RANK()
SELECT

DENSE_RANK(): This function gives same rank for duplicate values and does not give gaps between
ranks.

Eg: SELECT ENAME,DEPTNO,SAL,DENSE_RANK() OVER(ORDER BY SAL) DEN_RANK FROM EMP


OutPut:

ENAME DEPTNO SAL DEN_RANK


SMITH 20 800.00 1
JAMES 30 950.00 2
ADAMS 20 1100.00 3
WARD 30 1250.00 4
MARTIN 30 1250.00 4
ALLEN 30 1300.00 5
TURNER 30 1300.00 5
MILLER 10 1300.00 5
CLARK 10 2450.00 6
BLAKE 30 2850.00 7
JONES 20 2975.00 8
SCOTT 20 3000.00 9
FORD 20 3000.00 9
KING 10 5000.00 10

Here FROM clause takes the data from EMP table and ORDER BY clause keeps the data in sorting order
based on SAL column. Dense_Rank() gives numbers to the sorted data and it gives same number to
duplicate values but it does not give gaps. This new column is named as DEN_RANK. Finally SELECT
displays ENAME, DEPTNO, SAL and DEN_RANK columns.

Execution Steps:
FROM
ORDER BY
DENSE_RANK()
SELECT

Eg: SELECT ENAME,DEPTNO,SAL,DENSE_RANK() OVER(PARTITION BY DEPTNO ORDER BY SAL) DEN_RANK


FROM EMP

OUTPUT:
ENAME DEPTNO SAL DEN_RANK
MILLER 10 1300.00 1
CLARK 10 2450.00 2
KING 10 5000.00 3
SMITH 20 800.00 1
ADAMS 20 1100.00 2
JONES 20 2975.00 3
SCOTT 20 3000.00 4
FORD 20 3000.00 4
JAMES 30 950.00 1
WARD 30 1250.00 2
MARTIN 30 1250.00 2
ALLEN 30 1300.00 3
TURNER 30 1300.00 3
BLAKE 30 2850.00 4

Here FROM clause takes all data(all columns and all rows) from EMP table and PARTITION BY clause
divides the data into small partitions based on the DEPTNOs. ORDER BY clause keeps the data of each
partition in sorting order. DENSE_RANK() gives numbers for each partition starting from 1 and it also
gives same number for duplicate values and does not give gaps.

You might also like