// window reference

SQL CURRENT ROW frame boundary

As a frame boundary, CURRENT ROW is positional under ROWS and peer-inclusive under RANGE and GROUPS, which is where identical queries diverge.

Category
Window
Returns
frame boundary
In this category
54 of 79

Syntax

ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW | RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW

Engine support

EngineSupportNotes
SQL Server Yes Supported under this name.
PostgreSQL Yes Supported under this name.
MySQL Yes MySQL 8.0+
SQLite Yes SQLite 3.25+
Oracle Yes Supported under this name.

Examples

Any engine

-- tied days: RANGE version includes both rows' amounts at once
SELECT day, amount,
       SUM(amount) OVER (ORDER BY day ROWS UNBOUNDED PRECEDING) AS by_row,
       SUM(amount) OVER (ORDER BY day RANGE UNBOUNDED PRECEDING) AS by_peer
FROM t;

Good to know

  • With duplicate ORDER BY values, RANGE ... CURRENT ROW includes every peer, so running sums jump by the whole tie group; ROWS ... CURRENT ROW walks row by row.
  • This single word difference is the whole ROWS-vs-RANGE distinction; the default (implicit) frame uses RANGE semantics, which is where most surprises originate.
  • CURRENT ROW works as both the start and the end boundary; as a start it means 'this row onward' (plus peers under RANGE).

Questions about CURRENT ROW Boundary

Why does my running total skip ties one moment and include them the next?

Someone changed ROWS to RANGE (or removed it, falling back to the default RANGE). Under RANGE, CURRENT ROW carries the current row's peers with it.

Which CURRENT ROW should I use for dedupes and top-N?

Neither directly: ranking uses partitions, not frames. For running aggregates over unique-ordered rows, prefer ROWS plus a tiebreaker.

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.