Window Functions

Lesson 15 of 20

Rank rows without squashing them — watch RANK() OVER PARTITION stamp every employee with a per-department rank.

The big picture

Aggregate without collapsing

GROUP BY squashes each group into one row. Window functions compute over a group too — but every original row stays in the result, decorated with the calculation.

OVER opens a window

RANK() OVER (...) tells the engine: look at a set of rows, compute, then write the answer back onto each row.

PARTITION BY splits

PARTITION BY dept makes independent windows; every calculation restarts inside each one.

ORDER BY inside

ORDER BY salary DESC lines up the rows of a window so ranks can be assigned.

RANK vs ROW_NUMBER

ROW_NUMBER numbers 1, 2, 3 with no ties; RANK gives equal rows equal ranks and skips ahead after a tie.

SQL:2003

Standardized

Window functions became part of the official SQL standard.

2009

PostgreSQL 8.4

One of the first major engines to ship full window function support.

2018

MySQL 8.0

MySQL added OVER (PARTITION BY ...) — late but very welcome.