windows function
windows function
Windows
Function in SQL
https://www.linkedin.com/in/nivetha-anand/
1
01
https://www.linkedin.com/in/nivetha-anand/
2
02
3
03
https://www.linkedin.com/in/nivetha-anand/
4
04
Syntax
SELECT column_name,
FUNCTION_NAME(expression) OVER (
PARTITION BY column_name
ORDER BY column_name
ROWS/RANGE frame_specification)
https://www.linkedin.com/in/nivetha-anand/
5
05
Syntax
🛠 Breakdown of Syntax Components:
FUNCTION_NAME(expression) → The aggregation or ranking
function applied to the selected rows (e.g., SUM(), AVG(),
RANK(), LAG(), etc.).
OVER() → Specifies the window (or subset of rows) over
which the function operates.
PARTITION BY → (Optional) Divides the result set into
smaller groups before applying the function.
ORDER BY → (Optional) Defines the order of rows within
each partition.
ROWS/RANGE frame specification → (Optional) Defines a
subset of rows within the partition for calculations like
running totals or moving averages.
https://www.linkedin.com/in/nivetha-anand/
6
06
https://www.linkedin.com/in/nivetha-anand/
7
07
4️⃣ DENSE_RANK()
Use case: Assigns a continuous rank without gaps.
Example: Similar to RANK(), but if two students tie, the next rank
follows sequentially (1,1,2…).
5️⃣ ROW_NUMBER()
Use case: Assigns a unique row number within each partition.
Example: When selecting the latest transaction per customer.
https://www.linkedin.com/in/nivetha-anand/
8
08
Row Number()
📌 Problem:
Assign a unique row number to employees within
each department based on salary in descending
order.
https://www.linkedin.com/in/nivetha-anand/
9
09
https://www.linkedin.com/in/nivetha-anand/
10
10
SELECT
employee_id,
department,
salary,
ROW_NUMBER() OVER (PARTITION BY department
ORDER BY salary DESC) AS row_num
FROM employees;
https://www.linkedin.com/in/nivetha-anand/
11
11
✅ EXPECTED OUTPUT
https://www.linkedin.com/in/nivetha-anand/
12
08
DENSE_RANK()
📌 Problem
Rank employees within each department based
on salary in descending order. If two employees
have the same salary, they should receive the
same rank, and the next rank should NOT be
skipped.
https://www.linkedin.com/in/nivetha-anand/
13
09
https://www.linkedin.com/in/nivetha-anand/
14
06
SELECT
employee_id,
department,
salary,
DENSE_RANK() OVER (PARTITION BY
department ORDER BY salary DESC) AS
dense_rank
FROM employees;
https://www.linkedin.com/in/nivetha-anand/
15
09
https://www.linkedin.com/in/nivetha-anand/
16
07
Explanation:
In Sales:
The highest salary ($95,000) gets rank 1.
Two employees have $85,000, so they share rank 2.
The next salary ($72,000) gets rank 3 (no rank is
skipped).
In IT:
The highest salary ($120,000) gets rank 1.
Two employees have $110,000, so they share rank 2.
The next salary ($100,000) gets rank 3 (no rank is
skipped).
In HR:
The highest salary ($70,000) gets rank 1.
The next salary ($68,000) gets rank 2.
https://www.linkedin.com/in/nivetha-anand/
17
08
RANK()
📌 Problem
Rank employees within each department based
on salary in descending order. If two employees
have the same salary, they should receive the
same rank, but the next rank should be skipped.
https://www.linkedin.com/in/nivetha-anand/
18
09
https://www.linkedin.com/in/nivetha-anand/
19
10
SELECT
employee_id,
department,
salary,
RANK() OVER (PARTITION BY department ORDER
BY salary DESC) AS rank
FROM employees;
https://www.linkedin.com/in/nivetha-anand/
20
11
https://www.linkedin.com/in/nivetha-anand/
21
12
Explanation:
In Sales:
The highest salary ($95,000) gets rank 1.
Two employees have $85,000, so they share rank 2.
The next rank is 4 (rank 3 is skipped).
In IT:
The highest salary ($120,000) gets rank 1.
Two employees have $110,000, so they share rank 2.
The next salary ($100,000) gets rank 4 (rank 3 is
skipped).
In HR:
The highest salary ($70,000) gets rank 1.
The next salary ($68,000) gets rank 2.
https://www.linkedin.com/in/nivetha-anand/
22
13
https://www.linkedin.com/in/nivetha-anand/
23
14
https://www.linkedin.com/in/nivetha-anand/
24
Was this post
helpful to you?
Please like, share, save, comment and repost!
♻️
https://www.linkedin.com/in/nivetha-anand/
25