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.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

WhatsApp