MySQL + SQL · Lesson 79
Window Functions in MySQL
What are Window Functions?
Window functions perform calculations across a set of rows related to the current row, WITHOUT collapsing them into one (unlike GROUP BY). Available in MySQL 8+.
Example: Running Total
SELECT name, marks,
RANK() OVER (ORDER BY marks DESC) AS rank_pos,
AVG(marks) OVER () AS class_avg
FROM students;Each row keeps its detail AND gets a rank plus the class average alongside.
PARTITION BY
OVER (PARTITION BY class ORDER BY marks DESC) restarts the calculation for each class — like per-group windows.
Summary
- Window functions compute over related rows without collapsing them.
- Use OVER(); PARTITION BY makes per-group windows.
💻 Live Code Editor
Is page ki language (MYSQL) mein code likhein aur turant chalाएं — yahीं, bina kuch install kiye.Powered by OneCompiler. Agar editor na khule to yahान naye tab mein kholein.
💻 Live Code Editor
Is page ke program yahan ready hain — chalाएं, badlें aur seekhें. Bina kuch install kiye.
Powered by OneCompiler. Editor mein code apne aap aa jata hai — Run dabaakर output dekhें.
Agar load na ho to naye tab mein kholें.