SQL window functions let you calculate across related rows (rankings, running totals, the previous month’s value) without collapsing your results the way GROUP BY does. They are a favourite topic in data analyst interviews and one of the most useful tools for real reporting. If GROUP BY is new to you, read SQL for beginners first.

SQL window functions in one sentence
A window function looks at a “window” of rows related to the current row and returns a value for every row.
function_name() OVER (PARTITION BY ... ORDER BY ...)
- PARTITION BY splits rows into groups (like GROUP BY, but rows are kept)
- ORDER BY sets the order inside each group
We’ll use a monthly table, branch_sales(branch, month, revenue), with made-up figures.
1. ROW_NUMBER: number rows in order
SELECT branch, month, revenue,
ROW_NUMBER() OVER (PARTITION BY branch ORDER BY revenue DESC) AS rn
FROM branch_sales;
Filter rn = 1 (in a CTE) to get each branch’s best month.
2. RANK vs DENSE_RANK
| revenue | ROW_NUMBER | RANK | DENSE_RANK |
|---|---|---|---|
| 500 | 1 | 1 | 1 |
| 500 | 2 | 1 | 1 |
| 450 | 3 | 3 | 2 |
RANK skips numbers after ties; DENSE_RANK doesn’t; ROW_NUMBER always gives unique numbers.
3. LAG: compare with the previous row
SELECT branch, month, revenue,
revenue - LAG(revenue) OVER (PARTITION BY branch ORDER BY month) AS change
FROM branch_sales;
LEAD does the same with the next row. The first row in each partition returns NULL.
4. Running totals
SELECT branch, month, revenue,
SUM(revenue) OVER (PARTITION BY branch ORDER BY month
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS ytd_revenue
FROM branch_sales;
5. Moving averages
SELECT branch, month, revenue,
AVG(revenue) OVER (PARTITION BY branch ORDER BY month
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS avg_3_month
FROM branch_sales;
A three-month moving average smooths out noisy monthly figures.
6. Share of total
SELECT branch, SUM(revenue) AS revenue,
ROUND(100.0 * SUM(revenue) / SUM(SUM(revenue)) OVER (), 1) AS pct_of_total
FROM branch_sales
GROUP BY branch;
An empty OVER () means “the whole result”.
Common pitfalls
- You can’t use a window function in WHERE: wrap the query in a CTE, then filter
- Forgetting PARTITION BY, so rankings run across all branches
- Using RANK when you need exactly one row per group (use ROW_NUMBER)
- Leaving out an explicit frame (ROWS BETWEEN …) for running totals, which can give surprising results with tied dates
The PostgreSQL documentation on window functions is an excellent reference; the syntax is almost identical in SQL Server, MySQL 8 and BigQuery.
Practise with real questions
Try: top three products per branch; month-on-month growth; customers whose spend dropped two months in a row. SQL window functions are part of module 2 in the Datamac data analytics courses, and they come up often in data analyst interview questions.
FAQs
What is the difference between GROUP BY and window functions?
GROUP BY returns one row per group. SQL window functions keep every row and add the calculated value alongside it.
Do all databases support window functions?
PostgreSQL, SQL Server, Oracle, BigQuery, Snowflake and MySQL 8+ do. Very old MySQL versions don’t.
Are window functions slow?
They’re usually efficient, but on very large tables make sure the PARTITION BY and ORDER BY columns are indexed or clustered.
Keep reading
- SQL for Beginners: 10 Queries Every Data Analyst Uses
- How to Become a Data Analyst in Nigeria With No Experience
- VLOOKUP vs XLOOKUP: Which Should You Use in 2026?
Learn this live with Datamac
Datamac’s 12-month data analytics internship teaches Excel, SQL, Python, Power BI, Tableau and machine learning in live online classes twice a week, with real Nigerian datasets, two capstones and career support.
Apply for the internship · Compare courses · Ask a question on WhatsApp


