SQL Window Functions in MySQL: ROW_NUMBER, RANK, DENSE_RANK, Running Total, LAG and LEAD
Ranking students by grade, showing the top 3 products per category, computing a running total of sales, or comparing this month with last month... these are reports you will be asked to build in almost every school system, inventory system or capstone project. Many beginners solve them with complicated subqueries, or by pulling all the rows into PHP or Python and looping. There is a much cleaner way: SQL window functions.