⬅ Previous Topic
Common Table Expressions in SQLNext Topic ⮕
SQL Pivot and Unpivot⬅ Previous Topic
Common Table Expressions in SQLNext Topic ⮕
SQL Pivot and UnpivotImagine you’re analyzing student marks and need to assign ranks, compare with previous results, or compute running totals — all without collapsing your dataset using GROUP BY. That’s where Window Functions shine. They give you analytical power across rows while keeping each row intact.
Window functions perform calculations across a “window” of rows related to the current row. Unlike GROUP BY, they don’t collapse results. Instead, they add new analytical columns — such as ranks, moving averages, or differences — while preserving all rows.
function_name(...) OVER (
[PARTITION BY ...]
[ORDER BY ...]
)
CREATE TABLE marks (
roll_no INT,
name VARCHAR(50),
subject VARCHAR(30),
marks INT
);
INSERT INTO marks VALUES
(1, 'Aarav', 'Maths', 95),
(2, 'Diya', 'Maths', 85),
(3, 'Sneha', 'Maths', 92),
(4, 'Karan', 'Maths', 78),
(1, 'Aarav', 'Science', 90),
(2, 'Diya', 'Science', 80),
(3, 'Sneha', 'Science', 92),
(4, 'Karan', 'Science', 88);
SELECT name, subject, marks,
RANK() OVER (PARTITION BY subject ORDER BY marks DESC) AS rank
FROM marks;
name | subject | marks | rank
-------+---------+-------+------
Aarav | Maths | 95 | 1
Sneha | Maths | 92 | 2
Diya | Maths | 85 | 3
Karan | Maths | 78 | 4
Sneha | Science | 92 | 1
Aarav | Science | 90 | 2
Karan | Science | 88 | 3
Diya | Science | 80 | 4
SELECT name, subject, marks,
ROW_NUMBER() OVER (PARTITION BY subject ORDER BY marks DESC) AS row_num
FROM marks;
Unlike RANK(), this never gives duplicate numbers.
SELECT name, subject, marks,
DENSE_RANK() OVER (PARTITION BY subject ORDER BY marks DESC) AS dense_rank
FROM marks;
Perfect for scoreboards where tied ranks shouldn't leave gaps.
SELECT name, subject, marks,
LEAD(marks) OVER (PARTITION BY subject ORDER BY marks DESC) AS next_marks,
LAG(marks) OVER (PARTITION BY subject ORDER BY marks DESC) AS prev_marks
FROM marks;
name | subject | marks | next_marks | prev_marks
--------+---------+-------+------------+-------------
Aarav | Maths | 95 | 92 | NULL
Sneha | Maths | 92 | 85 | 95
Diya | Maths | 85 | 78 | 92
Karan | Maths | 78 | NULL | 85
Helps in comparisons like "How did this student score compared to others before/after?"
SELECT name, subject, marks,
SUM(marks) OVER (PARTITION BY subject ORDER BY marks DESC) AS cumulative_marks
FROM marks;
ORDER BY
clause in the windowPARTITION BY
to reset calculations per groupWindow functions are a powerful SQL feature that help you analyze data across rows without removing detail. Whether it’s building academic leaderboards, tracking score trends, or calculating cumulative performance, they add analytical depth with surgical precision — especially in school reporting systems where every row tells a story.
Next: Pivoting and Unpivoting Data — learn how to transform rows into columns and vice versa for better reporting.
⬅ Previous Topic
Common Table Expressions in SQLNext Topic ⮕
SQL Pivot and UnpivotYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.