0% found this document useful (0 votes)
11 views

10.window Functions

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

10.window Functions

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

DATABASE AND SQL

BY
JANARDHANA BANDI
WINDOW FUNCTIONS
WINDOW FUNCTIONS

A window is a group of related rows. A window can consist of one, or multiple rows.
A window function is any function that operates over a window of rows.

RANK – Returns rank over a group of values, skips the series in case of duplicates
DENSE_RANK – Returns rank over a group of values, doesn’t skips the series in case of
duplicates
ROW_NUMBER – Returns a unique row number for each row within a group of values
LEAD – To get the next row information
LAG – To get the next row information
FIRST_VALUE – Returns the first value within an ordered group of values.
LAST_VALUE – Returns the last value within an ordered group of values.
NTH_VALUE – Returns the nth value within an ordered group of values.
RANK
Rank:
• Returns the rank of a value within an ordered group of values.
• The rank value starts at 1 and continues up sequentially.
• If two values are the same, they have the same rank but skips the next
sequence number.
• Used to assign rank numbers, find the top values etc.

Syntax:

RANK() OVER ([partition by col/expr] order by col/expr [asc/desc])


DERIVE THE RANK BASED ON SALARY
FIRST_NA LAST_NA SALAR
EMPID ME ME AGE Y
1 Anitha Rao 28 60000
2 Vinay Kumar 31 70000
3 Divya Reddy 25 50000
4 Rama Devi 28 60000
5 Naresh Raju 30 55000 EMPI FIRST_NA LAST_NA SALAR
6 Uma Shankar 26 55000 D ME ME AGE Y Rank
7 Tilak Varma 29 60000 2 Vinay Kumar 31 70000 1
1 Anitha Rao 28 60000 2
SELECT EMPID, FIRST_NAME, 4 Rama Devi 28 60000 2
LAST_NAME, AGE, SALARY, 7 Tilak Varma 29 60000 2
5 Naresh Raju 30 55000 5
RANK() OVER(ORDER BY SALARY DESC) as 6 Uma Shankar 26 55000 5
Rank 3 Divya Reddy 25 50000 7
FROM EMPLOYEES;
DENSE_RANK

Desne_Rank:
• Returns the rank of a value within a group of values, without gaps in the ranks.
• The rank value starts at 1 and continues up sequentially.
• If two values are the same, they have the same rank, but doesn’t skips the next
sequence number.
• Used to assign rank numbers, find the top values etc.

Syntax:

DENSE_RANK() OVER ([partition by col/expr] order by col/expr [asc/desc])


DERIVE DENSE RANK BASED ON SALARY
FIRST_NA LAST_NA SALAR
EMPID ME ME AGE Y
1 Anitha Rao 28 60000
2 Vinay Kumar 31 70000
3 Divya Reddy 25 50000
4 Rama Devi 28 60000
5 Naresh Raju 30 55000 EMPI FIRST_NA LAST_NA SALAR
6 Uma Shankar 26 55000 D ME ME AGE Y DRank
7 Tilak Varma 29 60000 2 Vinay Kumar 31 70000 1
1 Anitha Rao 28 60000 2
SELECT EMPID, FIRST_NAME, LAST_NAME, 4 Rama Devi 28 60000 2
AGE, SALARY, 7 Tilak Varma 29 60000 2
5 Naresh Raju 30 55000 3
DENSE_RANK() OVER(ORDER BY SALARY
6 Uma Shankar 26 55000 3
DESC) as Rank
3 Divya Reddy 25 50000 4
FROM EMPLOYEES;
ROW_NUMBER

Row_Number:
• Returns a unique row number for each row within a window partition.
• The row number starts at 1 and continues up sequentially.
• If two values are the same, assigns row number randomly.
• Used to assign a sequence number irrespective of duplicates.

Syntax:

ROW_NUMBER() OVER ([partition by col/expr] order by col/expr [asc/desc])


DERIVE ROW NUMBER BASED ON SALARY
FIRST_NA LAST_NA SALAR
EMPID ME ME AGE Y
1 Anitha Rao 28 60000
2 Vinay Kumar 31 70000
3 Divya Reddy 25 50000
4 Rama Devi 28 60000
5 Naresh Raju 30 55000 EMPI FIRST_NA LAST_NA SALAR
D ME ME AGE Y Rank
6 Uma Shankar 26 55000
2 Vinay Kumar 31 70000 1
7 Tilak Varma 29 60000
7 Tilak Varma 29 60000 2
4 Rama Devi 28 60000 3
SELECT EMPID, FIRST_NAME, LAST_NAME, 1 Anitha Rao 28 60000 4
AGE, SALARY, 5 Naresh Raju 30 55000 5
ROW_NUMBER() OVER(ORDER BY SALARY 6 Uma Shankar 26 55000 6
DESC) as Rank 3 Divya Reddy 25 50000 7

FROM EMPLOYEES;
WHICH ONE TO CHOOSE?

It’s completely depends on our requirements.


1. Suppose if I want to find winners/toppers based on their score/marks then I have to choose
Dense_Rank for sure.
2. If I want to choose particular number of winners, I will go with Rank.
3. If I want to just generate a sequence number to rows based on some column/s, I will go with
Row Number.
4. If we are sure there are no duplicate values, we can choose any one but use Row Number to
avoid confusions.
LAG AND LEAD
LAG: Can fetch the value for a particular column from previous row in the same table/group
after sorting in some order, without using a self join.
Syntax: LAG ( col/expr, [ , <offset> , <default> ] )
OVER ( [ PARTITION BY <col/expr> ] ORDER BY <col/expr> [ { ASC | DESC } ] )

LEAD: Can fetch the value for a particular column from subsequent(next) row in the same
table/group after sorting in some order, without using a self join.
Syntax: LEAD ( col/expr, [ , <offset> , <default> ] )
OVER ( [ PARTITION BY <col/expr> ] ORDER BY <col/expr> [ { ASC | DESC } ] )
FIRST_VALUE, LAST_VALUE AND NTH_VALUE
FIRST_VALUE: Returns the first value within an ordered group of values.
Syntax: FIRST_VALUE( <col/expr> )
OVER ( [ PARTITION BY <col/expr> ] ORDER BY <col/expr> [ { ASC | DESC } ]

LAST_VALUE: Returns the last value within an ordered group of values.


Syntax: LAST_VALUE( <col/expr> )
OVER ( [ PARTITION BY <col/expr> ] ORDER BY <col/expr> [ { ASC | DESC } ]

NTH_VALUE: Returns the nth value within an ordered group of values.


Syntax: NTH_VALUE( <col/expr> , n)
OVER ( [ PARTITION BY <col/expr> ] ORDER BY <col/expr> [ { ASC | DESC } ]
Thank You

Janardhana Bandi

You might also like