Skip to main content
Albero Academy

SQL Window Functions

Master OVER, PARTITION BY, ROW_NUMBER, RANK, LAG and running totals — the most-asked topic in modern analytics interviews.

Chapter 3SQL17 min readBeginner friendly
  • #SQL
  • #Window Functions
  • #Analytics

Window functions are the most-asked topic in 2026 SQL interviews — and the single biggest unlock for analytics work. They let you compute aggregates and rankings without collapsing rows the way GROUP BY does.

What is a window function?

A window function performs a calculation across a set of rows ("the window") that are related to the current row. The output is still one row per input row — that's what makes it different from GROUP BY.

OVER and PARTITION BY

SQL
-- Show each employee's salary alongside their department's average
SELECT
name,
department,
salary,
AVG(salary) OVER (PARTITION BY department) AS dept_avg,
salary - AVG(salary) OVER (PARTITION BY department) AS diff_from_avg
FROM employees;

The OVER (PARTITION BY ...) clause defines the window. Without PARTITION BY, the window is the entire result set:

SQL
SELECT name, salary,
salary / SUM(salary) OVER () AS share_of_payroll
FROM employees;

ROW_NUMBER, RANK, DENSE_RANK

FunctionBehaviour on ties
ROW_NUMBER()always sequential — 1, 2, 3, 4
RANK()gaps after ties — 1, 2, 2, 4
DENSE_RANK()no gaps — 1, 2, 2, 3
SQL
SELECT name, department, salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn,
RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS rk,
DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS dr
FROM employees;

LAG and LEAD

Useful for time-series questions: "what was the previous value?" / "what's the next?"

SQL
-- Days between a user's logins
SELECT user_id, login_at,
LAG(login_at) OVER (PARTITION BY user_id ORDER BY login_at) AS prev_login,
login_at - LAG(login_at) OVER (PARTITION BY user_id ORDER BY login_at) AS gap
FROM logins;
-- Compare today's revenue to yesterday's
SELECT day, revenue,
LAG(revenue) OVER (ORDER BY day) AS yesterday,
revenue - LAG(revenue) OVER (ORDER BY day) AS day_over_day
FROM daily_metrics;

Running totals

SQL
SELECT order_date,
amount,
SUM(amount) OVER (
ORDER BY order_date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS running_total
FROM orders;

Top-N per group

The single most-asked SQL interview pattern: "find the 3 highest-paid employees per department."

SQL
WITH ranked AS (
SELECT name, department, salary,
DENSE_RANK() OVER (
PARTITION BY department
ORDER BY salary DESC
) AS rk
FROM employees
)
SELECT name, department, salary
FROM ranked
WHERE rk <= 3
ORDER BY department, salary DESC;

Key takeaways

  1. 01Window functions compute aggregates without collapsing rows.
  2. 02OVER (PARTITION BY ...) defines the window; ORDER BY orders rows within it.
  3. 03Use ROW_NUMBER for unique rankings, RANK / DENSE_RANK to handle ties.
  4. 04LAG/LEAD look at previous/next rows — perfect for time-series gaps.
  5. 05The Top-N-per-group pattern is the most-asked window question — memorise it.