Advanced SQL · 10 min read · Intermediate
Window Functions: ROW_NUMBER, RANK & Running Totals
Compute rankings, top-N per group, running totals, and moving averages without collapsing rows via OVER (PARTITION BY ...).
What you'll learn
- Distinguish GROUP BY aggregation from window functions
- Use ROW_NUMBER(), RANK(), and DENSE_RANK()
- Calculate running totals with SUM() OVER (ORDER BY ...)
Overview
Compute rankings, top-N per group, running totals, and moving averages without collapsing rows via OVER (PARTITION BY …).
Strong database design pays dividends for years: clean constraints catch bugs before application code does, and well-chosen indexes keep response times predictable as tables grow from thousands to millions of rows.
Try It Live (Interactive SQL Sandbox)
SQLWindow Functions: ROW_NUMBER, RANK & Running Totals
Query Result (In-Browser SQLite)
Click Run SQL to execute this query against a live in-browser SQLite database.
Key Takeaways
- Distinguish GROUP BY aggregation from window functions
- Use ROW_NUMBER(), RANK(), and DENSE_RANK()
- Calculate running totals with SUM() OVER (ORDER BY …)
Quick Check
Check Your Understanding
Score: 0 / 1
1. How does a window function (`OVER (...)`) differ from `GROUP BY`?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.