Window Functions with OVER()

Window topicPurpose
OVER() / PARTITION BYAggregate values while preserving each row.
RANK(), DENSE_RANK(), ROW_NUMBER(), NTILE()Rank and number rows within a window.
LEAD() / LAG()Compare current rows with following or previous rows.
OVER() with Partition

GROUP BY collapses rows into one result row per group. A window function uses OVER() to calculate across a set of rows while keeping each original result row visible.



We may want to list all rows and at the same time display grouped result. Say along with individual rows with marks the sum of grouped (aggregate ) mark of the full result can be displayed.
Check this SQL.
SELECT class, SUM(mark) AS total
FROM student
WHERE class = 'Three'
GROUP BY class;
Output is one row for the selected class.
classtotal
Three221
Window functions are available in MySQL 8.0 and later.

Using OVER()

SELECT id, name, class, mark, gender,
       SUM(mark) OVER () AS total
FROM student
WHERE class = 'Three';
Output is here ( watch the total column which is single global sum for all rows taken as a group and shown against each row of result )
idnameclassmarkgendertotal
2Max RuinThree85male221
3ArnoldThree55male221
27Big NoseThree81female221
Aggregate functions such as AVG(), MAX(), MIN(), SUM() and COUNT() can also operate as window functions when an OVER() clause is present.

Here we are trying to display each student mark and compare it with aggregate over another column.
SELECT id, name, class, mark, gender,
       SUM(mark) OVER () AS total,
       AVG(mark) OVER () AS average_mark,
       MAX(mark) OVER () AS highest_mark,
       MIN(mark) OVER () AS lowest_mark
FROM student
WHERE class = 'Three';
Output
idnameclassmarkgendertotalavgmaxmin
2Max RuinThree85male22173.6678555
3ArnoldThree55male22173.6678555
27Big NoseThree81female22173.6678555

Using PARTITION BY

With an empty OVER(), all selected rows form one window. PARTITION BY divides them into smaller windows without collapsing the rows.
SELECT name, class, mark,
       SUM(mark) OVER () AS total,
       SUM(mark) OVER (PARTITION BY class) AS class_total
FROM student
WHERE id < 10;
Output : The first OVER() ( total) gives us sum of the total collection of result, the second OVER() ( class_total ) gives us sum by grouping the result across the class.
idnameclassmarkgendertotalclass_total
7My John RobFive78male631163
8AsruidFive85male631163
1John DeoFour75female631250
4Krish StarFour60female631250
5John MikeFour60female631250
6Alex JohnFour55male631250
9Tes QrySix78male63178
2Max RuinThree85male631140
3ArnoldThree55male631140
We can further group result in more than one column.
SELECT id, name, class, mark, gender,
       SUM(mark) OVER () AS total,
       SUM(mark) OVER (PARTITION BY class, gender) AS class_gender_total
FROM student
WHERE id < 20;
To sort the final result without changing the window calculation, use an outer ORDER BY. Putting ORDER BY inside an aggregate window definition can change the window frame and therefore the calculated value.
SELECT id, name, class, mark, gender,
       SUM(mark) OVER () AS total,
       SUM(mark) OVER (PARTITION BY class, gender) AS class_gender_total
FROM student
WHERE id < 20
ORDER BY class, gender, mark;

Aggregate windows functions

AVG() BIT_AND() BIT_OR() BIT_XOR() COUNT() JSON_ARRAYAGG() JSON_OBJECTAGG() MAX() MIN() STDDEV_POP(), STDDEV(), STD() STDDEV_SAMP() SUM() VAR_POP(), VARIANCE() VAR_SAMP()

nonaggregate functions

CUME_DIST() FIRST_VALUE() LAST_VALUE() LAG(),LEAD()
NTH_VALUE() PERCENT_RANK()
RANK(),DENSE_RANK(),ROW_NUMBER(),NTILE()

SQL Math References Sum query Sum Multiple column


Subscribe to our YouTube Channel here



plus2net.com




SQL Video Tutorials










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer