7 MIN · 2026-08-04

Window functions, introduced gently

Rankings, running totals and rows that see other rows — the window functions superpower, explained without the jargon.

Window functions, introduced gently

Here's the one-sentence idea: window functions are rows that see other rows. That's the whole superpower.

Until now, every tool you know works on one row at a time. A window function changes that: the row stays, but while SQL computes its answer, the row is allowed to look at its neighbors — the ones before it, the ones after it, even all of them at once. No jargon needed, just examples from the record store.

The basic move: OVER

The easiest window function is ROW_NUMBER() — it counts rows from 1. On its own it's useless; the interesting part is the OVER (...) clause that follows it:

SELECT title, duration_sec,
       ROW_NUMBER() OVER (ORDER BY duration_sec DESC) AS position
FROM tracks
ORDER BY duration_sec DESC;

Every track stays in the result — that's the difference from GROUP BY, which would have crushed them into summaries — and each one gains a position:

titleduration_secposition
Nocturne No. 24021
Nocturne No. 13892
Crossroads (Live)3563
Meridian Blue3424
Low Orbit3425

The OVER (ORDER BY ...) decides what "numbered from the top" means. Each row could only see itself before; now it knows where it stands in the whole list.

RANK: sharing a tie

ROW_NUMBER() never shares: two tied rows get different numbers. Sometimes that's unfair — if two things are equal, they deserve the same position. That's what RANK() is for:

SELECT billing_city, total,
       RANK() OVER (ORDER BY total DESC) AS rank,
       ROW_NUMBER() OVER (ORDER BY total DESC) AS row_num
FROM invoices
ORDER BY total DESC;

Two invoices total 3.27 (one from Austin, one from Madrid). RANK() gives both the same position — 4th — and skips 5th, jumping to 6 for the next invoice. ROW_NUMBER() just numbers them: 4, then 5. Same story lower down, where two 2.97 invoices (Lyon and São Paulo) share rank 6.

Think of it as two different answers: RANK says "there are 3 totals bigger than this one"; ROW_NUMBER says "you are literally the 5th row".

The running total: a sum that never stops

Here's the moment it clicks for most people. You know SUM(total) — it adds a column and returns one number. Add OVER and the same SUM stops collapsing:

SELECT invoice_date, total,
       SUM(total) OVER (ORDER BY invoice_date) AS running_total
FROM invoices;

Now every row carries the sum of all rows up to and including itself, in date order. The first invoice: 2.58. The second: 2.58 + 4.56 = 7.14. Then 8.43, 11.40, and on and on until the last row reads 53.61 — the store's entire takings. Same SUM you already know; the window is what changed.

Rows that see other rows

The window doesn't have to be "everything so far" — it can be everything, period. OVER () with empty parentheses means the whole table. So each row can compare itself against the grand total:

SELECT invoice_date, total,
       ROUND(100.0 * total / SUM(total) OVER (), 1) AS share
FROM invoices;

The first invoice's 2.58 is 4.8% of the store's total sales. Every row computes its own share — but the denominator, SUM(total) OVER (), is the same number for all of them: 53.61. A single row suddenly knows where it fits in the universe of the table.

Takeaway

Window functions are how you write rankings, running totals and "how does this row compare to everything else" without losing a single row. Nothing collapses, nothing disappears — each row just gains eyes.

Keep exploring: the window functions chapter takes this further — rankings inside each genre with PARTITION BY, days between invoices with LAG, and moving averages.

Keep learning