The Idea
An aggregate with GROUP BY collapses rows. A window function keeps every row and adds a calculated column. OVER (...) defines the window.
Result:
| name | category | price | category_avg |
|---|---|---|---|
| Headphones | Electronics | 79.00 | 525.67 |
| Laptop | Electronics | 899.00 | 525.67 |
| Phone | Electronics | 599.00 | 525.67 |
| Chair | Furniture | 129.00 | 141.00 |
| Desk | Furniture | 249.00 | 141.00 |
| Lamp | Furniture | 45.00 | 141.00 |
| Notebook | Stationery | 3.50 | 4.25 |
| Pen Pack | Stationery | 5.00 | 4.25 |
Ranking
Result:
| name | category | price | rn | rnk | drnk |
|---|---|---|---|---|---|
| Laptop | Electronics | 899.00 | 1 | 1 | 1 |
| Phone | Electronics | 599.00 | 2 | 2 | 2 |
| Headphones | Electronics | 79.00 | 3 | 3 | 3 |
| Desk | Furniture | 249.00 | 1 | 1 | 1 |
| Chair | Furniture | 129.00 | 2 | 2 | 2 |
| Lamp | Furniture | 45.00 | 3 | 3 | 3 |
| Pen Pack | Stationery | 5.00 | 1 | 1 | 1 |
| Notebook | Stationery | 3.50 | 2 | 2 | 2 |
With ties, ROW_NUMBER still gives different numbers, RANK leaves gaps (1, 1, 3), and DENSE_RANK does not (1, 1, 2). Try it with tied values:
Result:
| name | pts | rn | rnk | drnk |
|---|---|---|---|---|
| A | 90 | 1 | 1 | 1 |
| B | 90 | 2 | 1 | 1 |
| C | 80 | 3 | 3 | 2 |
| D | 70 | 4 | 4 | 3 |
Top N per Group
A window function cannot be used in WHERE, so wrap it in a derived table or CTE:
Result:
| category | name | price |
|---|---|---|
| Electronics | Laptop | 899.00 |
| Electronics | Phone | 599.00 |
| Furniture | Desk | 249.00 |
| Furniture | Chair | 129.00 |
| Stationery | Pen Pack | 5.00 |
| Stationery | Notebook | 3.50 |
Running Totals and Moving Averages
Result:
| id | order_date | status | orders_so_far |
|---|---|---|---|
| 1 | 2025-01-10 | shipped | 1 |
| 2 | 2025-02-14 | shipped | 2 |
| 3 | 2025-02-20 | shipped | 3 |
| 4 | 2025-03-01 | pending | 4 |
| 5 | 2025-03-15 | cancelled | 5 |
| 6 | 2025-04-02 | shipped | 6 |
| 7 | 2025-04-20 | pending | 7 |
| 8 | 2025-05-05 | shipped | 8 |
| 9 | 2025-05-11 | shipped | 9 |
| 10 | 2025-06-01 | shipped | 10 |
Running revenue needs the order totals. Aggregate first, then window over the result:
Result:
| order_date | revenue | running_revenue |
|---|---|---|
| 2025-01-10 | 1057.00 | 1057.00 |
| 2025-02-14 | 55.00 | 1112.00 |
| 2025-02-20 | 599.00 | 1711.00 |
| 2025-03-01 | 507.00 | 2218.00 |
| 2025-04-02 | 989.00 | 3207.00 |
| 2025-04-20 | 237.00 | 3444.00 |
| 2025-05-05 | 199.00 | 3643.00 |
| 2025-05-11 | 678.00 | 4321.00 |
| 2025-06-01 | 60.00 | 4381.00 |
LAG and LEAD: Compare with a Neighbour
Result:
| order_date | revenue | previous | diff |
|---|---|---|---|
| 2025-01-10 | 1057.00 | NULL | 1057.00 |
| 2025-02-14 | 55.00 | 1057.00 | -1002.00 |
| 2025-02-20 | 599.00 | 55.00 | 544.00 |
| 2025-03-01 | 507.00 | 599.00 | -92.00 |
| 2025-04-02 | 989.00 | 507.00 | 482.00 |
| 2025-04-20 | 237.00 | 989.00 | -752.00 |
| 2025-05-05 | 199.00 | 237.00 | -38.00 |
| 2025-05-11 | 678.00 | 199.00 | 479.00 |
| 2025-06-01 | 60.00 | 678.00 | -618.00 |
LAG(x, 1, 0) looks one row back and uses 0 when there is no earlier row. LEAD looks forward.
Frames: Moving Windows
Result:
| order_date | revenue | avg_last_two |
|---|---|---|
| 2025-01-10 | 1057.00 | 1057.00 |
| 2025-02-14 | 55.00 | 556.00 |
| 2025-02-20 | 599.00 | 327.00 |
| 2025-03-01 | 507.00 | 553.00 |
| 2025-04-02 | 989.00 | 748.00 |
| 2025-04-20 | 237.00 | 613.00 |
| 2025-05-05 | 199.00 | 218.00 |
| 2025-05-11 | 678.00 | 438.50 |
| 2025-06-01 | 60.00 | 369.00 |
NTILE, FIRST_VALUE and Named Windows
Result:
| name | price | half | cheapest_name |
|---|---|---|---|
| Notebook | 3.50 | 1 | Notebook |
| Pen Pack | 5.00 | 1 | Notebook |
| Lamp | 45.00 | 1 | Notebook |
| Headphones | 79.00 | 1 | Notebook |
| Chair | 129.00 | 2 | Notebook |
| Desk | 249.00 | 2 | Notebook |
| Phone | 599.00 | 2 | Notebook |
| Laptop | 899.00 | 2 | Notebook |
WINDOW w AS (...) names a window so you can reuse it.
ORDER BY and no frame, the default frame runs to the current row including ties, so LAST_VALUE often returns the current row. Write the frame explicitly: ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.
Common Beginner Mistakes
| Mistake | What happens | Fix |
|---|---|---|
Window function in WHERE | Error | Wrap in a CTE or derived table |
Using RANK for top 1 | Ties return many rows | Use ROW_NUMBER with a tie-breaker |
No tie-breaker in ORDER BY | Unstable numbers | Add a unique column |
| Using these on MySQL 5.7 | Syntax error | Upgrade to 8.0 |
AS rank | Reserved word error | Pick another alias |
Practice
- Rank customers by total spend.
- Find each customer's most recent order with
ROW_NUMBER. - Show days between a customer's orders with
LAG. - Compute a 3-row moving average.
Conclusion
Window functions add a calculation per row with OVER. Use PARTITION BY for groups, ORDER BY for sequence, ROW_NUMBER/RANK for ranking, LAG/LEAD for neighbours and frames for moving totals.

