0% found this document useful (0 votes)
7 views26 pages

Window Functions in SQL

Uploaded by

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

Window Functions in SQL

Uploaded by

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

Window Functions in

SQL
• SQL window functions are essential for advanced data analysis and
database management. It is a type of function that allows us to
perform calculations across a specific set of rows related to the
current row. These calculations happen within a defined window of
data and they are particularly useful for aggregates, rankings and
cumulative totals without modifying the dataset.
• The OVER clause is a key for defining this window. It partitions the
data into different sets (using the PARTITION BY clause) and orders
them (using the ORDER BY clause). These windows enable functions
like SUM(), AVG(), ROW_NUMBER(), RANK() and DENSE_RANK() to be
applied in an organized manner.
• Syntax

SELECT column_name1,
window_function(column_name2)
OVER([PARTITION BY column_name1] [ORDER BY column_name3]) AS
new_column
FROM table_name;
Types of Window Functions in
SQL
• aggregate window functions and ranking window functions.
1. Aggregate Window Function

• Aggregate window functions calculate aggregates over a window of


rows while retaining individual rows. Common aggregate functions
include:
• SUM(): Sums values within a window.
• AVG(): Calculates the average value within a window.
• COUNT(): Counts the rows within a window.
• MAX(): Returns the maximum value in the window.
• MIN(): Returns the minimum value in the window.
Using AVG() to Calculate the Average Salary
within each department

SELECT Name, Age, Department, Salary,


AVG(Salary) OVER( PARTITION BY Department) AS Avg_Salary
FROM employee
Output

•The AVG() function calculates the average salary for each department using the PARTITION
BY Department clause.

•The average salary is repeated for all rows in the respective department.
2. Ranking Window Functions
• These functions provide rankings of rows within a partition based on
specific criteria. Common ranking functions include:
• RANK(): Assigns ranks to rows, skipping ranks for duplicates.
• DENSE_RANK(): Assigns ranks to rows without skipping rank numbers for
duplicates.
• ROW_NUMBER(): Assigns a unique number to each row in the result set.
RANK() Function

• The RANK() function assigns ranks to rows within a partition, with the
same rank given to rows with identical values. If two rows share the
same rank, the next rank is skipped.
Using RANK() to Rank Employees by
Salary
SELECT Name, Department, Salary,
RANK() OVER(PARTITION BY Department ORDER BY Salary DESC) AS
emp_rank
FROM employee;
DENSE_RANK() Function

• It assigns rank to each row within partition. Just like rank function first
row is assigned rank 1 and rows having same value have same rank.
The difference between RANK() and DENSE_RANK() is that
in DENSE_RANK(), for the next rank after two same rank, consecutive
integer is used, no rank is skipped.
SELECT Name, Department, Salary,
DENSE_RANK() OVER(PARTITION BY Department ORDER BY Salary DESC) AS
emp_dense_rank
FROM employee;
Name Department Salary emp_dense_rank

Ramesh Finance 50,000 1

Suresh Finance 50,000 1

Ram Finance 20,000 2

Deep Sales 30,000 1

Pradeep Sales 20,000 2


ROW_NUMBER() Function
• ROW_NUMBER() gives e­ach row a unique number. It numbers rows from
one­ to the total rows. The rows are put into groups base­d on their
values. Each group is called a partition. In e­ach partition, rows get
numbers one afte­r another. No two rows have the same­ number in a
partition.

• Example: Using ROW_NUMBER() for Unique Row Numbers

SELECT Name, Department, Salary,


ROW_NUMBER() OVER(PARTITION BY Department ORDER BY Salary
DESC) AS emp_row_no
FROM employee;
Output

Name Department Salary emp_row_no


Ramesh Finance 50,000 1
Suresh Finance 50,000 2
Ram Finance 20,000 3
Deep Sales 30,000 1
Pradeep Sales 20,000 2
Practical Use Cases for Window
Functions
• Example 1: Calculating Running Totals
We want to calculate a running total of sales for each day without
resetting the total, every time a new day starts.

SELECT Date, Sales,


SUM(Sales) OVER(ORDER BY Date) AS Running_Total
FROM sales_data;

This query calculates the cumulative total of sales for each day, ordered
by date.
Example 2: Finding Top N Values in Each
Category
• We need to retrieve the top 3 employees in each department based on their salary.

WITH RankedEmployees AS (
SELECT Name, Department, Salary,
RANK() OVER(PARTITION BY Department ORDER BY Salary DESC) AS emp_rank
FROM employee
)
SELECT Name, Department, Salary
FROM RankedEmployees
WHERE emp_rank <= 3;
This query retrieves the top 3 employees per department based on salary. It uses
RANK() to rank employees within each department and filters for the top 3
Practice on Customers table
SELECT
customer_id,
first_name,
country,
age,
RANK() OVER (PARTITION BY country ORDER BY age desc ) as ranking
FROM
customers;
When to Use Which?

• ✅ Use RANK() when gaps in ranking are acceptable and expected.


• ✅ Use DENSE_RANK() when you want continuous rankings (like top N
positions without gaps).
🎯 Scenario:
• Suppose in a dataset, a partition (like for a particular department or
region) contains 3 employees, all with the same salary of 50000.

• RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC)


• DENSE_RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC)
• ROW_NUMBER() OVER (PARTITION BY dept_id ORDER BY salary DESC)
✅ RANK():
•All three rows have the same salary, so they get same rank = 1
•But the next distinct salary would get rank 4, because 3 rows shared rank 1 (i.e., it skips 2 and 3)

EmpID Salary RANK


101 50000 1
102 50000 1
103 50000 1
✅ DENSE_RANK():
•All three rows still get rank = 1
•The next distinct salary (if any) would get rank = 2 (no gaps)

EmpID Salary DENSE_RANK

101 50000 1

102 50000 1

103 50000 1
✅ ROW_NUMBER():
•Regardless of ties, each row gets a unique number based on row order

EmpID Salary ROW_NUMBER


101 50000 1
102 50000 2
103 50000 3

You might also like