0% found this document useful (0 votes)
2 views4 pages

window function new

The document provides a sample student table with student IDs, names, subjects, and marks. It demonstrates various SQL window functions, including aggregate functions (SUM, AVG), ranking functions (RANK, ROW_NUMBER), and value functions (LAG, LEAD, FIRST_VALUE), along with their corresponding SQL queries and outputs. Additionally, it includes an example of a cumulative sum using the SUM function with a window frame specification.

Uploaded by

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

window function new

The document provides a sample student table with student IDs, names, subjects, and marks. It demonstrates various SQL window functions, including aggregate functions (SUM, AVG), ranking functions (RANK, ROW_NUMBER), and value functions (LAG, LEAD, FIRST_VALUE), along with their corresponding SQL queries and outputs. Additionally, it includes an example of a cumulative sum using the SUM function with a window frame specification.

Uploaded by

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

Sample Student Table

StudentID Name Subject Marks

1 Alice Math 85

2 Bob Math 90

3 Carol Math 75

4 Dave Math 95

5 Eve Math 80

Aggregate Window Functions


1. SUM()

SELECT Name, Subject, Marks,


SUM(Marks) OVER (PARTITION BY Subject) AS TotalMarks
FROM Students;

Output:
Name Subject Marks TotalMarks

Alice Math 85 425

Bob Math 90 425

Carol Math 75 425

Dave Math 95 425

Eve Math 80 425


2. AVG()

SELECT Name, Subject, Marks,


AVG(Marks) OVER (PARTITION BY Subject) AS AvgMarks
FROM Students;

Output:
Name Subject Marks AvgMarks

Alice Math 85 85.0

Bob Math 90 85.0

Carol Math 75 85.0


Name Subject Marks AvgMarks

Dave Math 95 85.0

Eve Math 80 85.0

Ranking Window Functions


1. RANK()

SELECT Name, Subject, Marks,


RANK() OVER (PARTITION BY Subject ORDER BY Marks DESC) AS Rank
FROM Students;

Output:
Name Subject Marks Rank

Dave Math 95 1

Bob Math 90 2

Alice Math 85 3

Eve Math 80 4

Carol Math 75 5
2. ROW_NUMBER()

SELECT Name, Subject, Marks,


ROW_NUMBER() OVER (
PARTITION BY Subject
ORDER BY Marks DESC) AS RowNum
FROM Students;

Output:
Name Subject Marks RowNum

Dave Math 95 1

Bob Math 90 2

Alice Math 85 3

Eve Math 80 4

Carol Math 75 5
Value Window Functions
1. LAG()

SELECT Name, Subject, Marks,


LAG(Marks, 1) OVER (
PARTITION BY Subject
ORDER BY Marks) AS PrevMarks
FROM Students;

Output:
Name Subject Marks PrevMarks

Carol Math 75 NULL

Eve Math 80 75

Alice Math 85 80

Bob Math 90 85

Dave Math 95 90
2. LEAD()

SELECT Name, Subject, Marks,


LEAD(Marks, 1) OVER (
PARTITION BY Subject
ORDER BY Marks) AS NextMarks
FROM Students;

Output:
Name Subject Marks NextMarks

Carol Math 75 80

Eve Math 80 85

Alice Math 85 90

Bob Math 90 95

Dave Math 95 NULL


Value Window Functions
1. FIRST_VALUE()

SELECT Name, Subject, Marks,


FIRST_VALUE(Marks) OVER (
PARTITION BY Subject
ORDER BY Marks) AS FirstMarks
FROM Students;

Output:
Name Subject Marks FirstMarks

Carol Math 75 75

Eve Math 80 75

Alice Math 85 75

Bob Math 90 75

Dave Math 95 75

Aggregate Window Functions


1. Cumulative SUM()

SELECT Name, Subject, Marks,


SUM(Marks) OVER (PARTITION BY Subject ORDER BY Marks ROWS BETWEEN
UNBOUNDED PRECEDING AND CURRENT ROW) AS CumulativeSum
FROM Students;

Output:
Name Subject Marks CumulativeSum

Carol Math 75 75

Eve Math 80 155

Alice Math 85 240

Bob Math 90 330

Dave Math 95 425

You might also like