0% found this document useful (0 votes)
5 views3 pages

2023.08.25-08.30AM-SQL - BQ-Windows Function (Rank, Dense - Rank, Row - Number)

The document discusses various SQL window functions such as Rank(), Dense_Rank(), and Row_num(), explaining their usage and differences, particularly in handling duplicate values. It includes examples of using these functions to calculate sums over specified ranges of records. Additionally, it touches on real-time project use cases and class management details for a training program.

Uploaded by

Shantanu Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

2023.08.25-08.30AM-SQL - BQ-Windows Function (Rank, Dense - Rank, Row - Number)

The document discusses various SQL window functions such as Rank(), Dense_Rank(), and Row_num(), explaining their usage and differences, particularly in handling duplicate values. It includes examples of using these functions to calculate sums over specified ranges of records. Additionally, it touches on real-time project use cases and class management details for a training program.

Uploaded by

Shantanu Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Windows Functions:

Rank() --> To find out rank based on a particular column we can use Rank function

Rank() over(order by columnname asc) --> It will ranks from lowest value to highest
values
Rank() over(order by columnname desc) --> It will ranks from highest value to
lowest values

If you want to find our rank for a particular partition --> Partiton by

Rank() over(Partition by columnname order by columnname desc)

*******************************************************************************

Dense_RANK():
Both Rank and Dense_Rank are going to work in same way. Only difference if column
is having duplicate values in that situation rank() and dense_rank() with givve
different kind of results.

Rank=dense_rank ==> If column is not having any duplicate value


Rank<>dense_rank ==> If column is having any duplicate values

while using rank function --> if rank column is having any duplicates, it will
populate duplicate rank values by skipping the ranks

while using dense_rank function --> if desne rank column is having any duplicates,
it will populate duplicate rank values with out skipping the ranks

Row_num() --> Whenever you want to populate sequence values then we can use
row_num() function

row_num=rank=dense_rank ==> If column is not having duplicates


row_num<>rank<>dense_rank ==> If column is having any duplicates

Select eid,ename,sal,sum(sal) over(order by sal asc


rows between unbounded preceding and unbounded following) as sal_x from emp;

==> sum with current records and previous 1 record:

Select eid,ename,sal,sum(sal) over(order by sal asc


rows between 1 preceding and 0 following) as sal_x from sampleqt.employee_main
order by sal asc;

==> sum with current record and previous 2 record:

Select eid,ename,sal,sum(sal) over(order by sal asc


rows between 2 preceding and 0 following) as sal_x from sampleqt.employee_main
order by sal asc;

==> sum with current record and previous 2 records and next 2 records:

Select eid,ename,sal,sum(sal) over(order by sal asc


rows between 2 preceding and 2 following) as sal_x from sampleqt.employee_main
order by sal asc;

Select eid,ename,sal,sum(sal) over(order by sal asc


rows between unbounded preceding and unbounded following) as sal_x from
sampleqt.employee_main
order by sal asc;

Select eid,ename,sal,sum(sal) over(order by sal asc


rows between unbounded preceding and unbounded following) as sal_x from
sampleqt.employee_main
order by sal asc;

current record+2 prev records+2 following records

unbounded - All records


preceeding - 0 --> Current record
following - 2

we can able to handle our sum operations with window functionality

***********************************************************************************
**

real time
Banking projects
Loan
EMI

insurance projects

Customer paying premium amount every quarter

custid premiumnumber premiumamount


1 q1 10000
1 q2 11000
1 q3 10000
1 q4 10000
2
2
2
2

Sum in the combiantion of preceeding and foloowing and over --> window function
sum --> aggregate function
***********************************************************************************
*************

2 3 mins -->

Bigquery --> Usecase to you --> please work on this usecase by today EOD [1 or 2
hours]

All the details of the usecase

If we are going to work in real time --> whether any one will explain requirement
to you

22 steps whatever we discussed

Complete usecase

You want me to explain end to end ???? []


***********************************************************************************
*************
11:45 we dont have any class --> 2PM

sql 3rd assignment ready --> I will also share


***********************************************************************************
*************

Mock interviews [you will get your slot time, admin team will call you and they
will inform your slot time]
Classes

***********************************************************************************
*************
Dont worry about below things, I wil take care. Old batch people dont expect to
complete classes ASAP, I need to take care of New batch people also.

1. new topic
2. when I am going to complete classes
3. whether all the topics are covered or not
4. we have only 20 days can we cover all the topics

You might also like