ROW_NUMBER, RANK, and SUM OVER () arrived in MySQL 8—verify VERSION() before using in production mixed fleets.
Ranking
SELECT id, customer_id, total,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY total DESC) AS rn
FROM orders;Practice: Run on practice.
Running total
SELECT id, total,
SUM(total) OVER (ORDER BY created_at) AS running
FROM orders;
Important interview questions and answers
- Q: PARTITION BY?
A: Resets window per group—like GROUP BY but keeps detail rows. - Q: MariaDB version?
A: MariaDB 10.2+ supports window functions—check docs for syntax edge cases.
Self-check
- Rank orders per customer by total.
- MySQL version for window functions?
Tip: Confirm VERSION() >= 8 before window functions in prod.
Interview prep
- MySQL version?
8.0+ for window functions.
- PARTITION BY?
Defines per-group window.