IT lexicon › Database › Window function

Window function

Database På svenska → Updated: 2026-07-30

A SQL function that computes a value over a set of rows ("window") related to the current row, without collapsing them the way GROUP BY does. Enables row numbering, running totals, ranking, and comparisons with the previous/next row.

Syntax: function() OVER (PARTITION BY ... ORDER BY ...). Common functions: ROW_NUMBER(), RANK(), DENSE_RANK(), LAG()/LEAD() (look at the previous/next row), SUM() OVER (...) (running total). The difference from an aggregate + GROUP BY: window functions KEEP all rows and add a computed column → "show each order plus the customer's total spending on the same row". Use case: "top-N per group", running balances, percent of total, gap detection. A SQL:2003 standard, supported by all modern databases. One of the most underused powerful SQL features — it often replaces awkward self-joins.

← Back to the lexicon