// window reference

SQL default window frame

The implicit frame covers the partition up to and including the current row's peers, which is why LAST_VALUE disappoints and running sums include ties.

Kategorie
Fenster
Returns
implicit frame
In this category
50 of 79

Syntax

SUM(x) OVER (ORDER BY d) | -- implicitly:
SUM(x) OVER (ORDER BY d RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)

Engine-Unterstützung

EngineSupportNotizen
SQL Server Ja Supported under this name.
PostgreSQL Ja Supported under this name.
MySQL Ja MySQL 8.0+
SQLite Ja SQLite 3.25+
Oracle Ja Supported under this name.

Beispiele

Any engine

-- both rows of a tie show 30, not 10 then 30
-- amounts: 10, 20 on the same day
SELECT day, amount,
       SUM(amount) OVER (ORDER BY day) AS running
FROM t;
-- explicit form for row-by-row accumulation:
SELECT day, amount,
       SUM(amount) OVER (ORDER BY day, id ROWS UNBOUNDED PRECEDING) AS running_strict
FROM t;

Gut zu wissen

  • Without ORDER BY there is no frame at all: the aggregate covers the entire partition. Adding ORDER BY is what switches on the default frame.
  • Peers matter: rows sharing all ORDER BY values are in the same frame end, so tied rows show identical running aggregates: sometimes the intent, often a bug.
  • Offset functions are unaffected: LAG/LEAD ignore the frame entirely, while FIRST_VALUE/LAST_VALUE/NTH_VALUE respect it.

Questions about Default Window Frame

What exactly is the default frame?

When OVER has ORDER BY: RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW (start of partition through the current row plus its peers). Without ORDER BY: the whole partition.

How do I make the default frame row-by-row?

Declare ROWS instead of RANGE (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) and add a unique tiebreaker to the ORDER BY.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.