// window reference

SQL frame shorthand syntax

Writing just a start boundary (ROWS UNBOUNDED PRECEDING) omits BETWEEN, and the frame then ends at CURRENT ROW by definition.

Categoria
Janela
Returns
frame specification
In this category
55 of 79

Sintaxe

ROWS n PRECEDING | RANGE UNBOUNDED PRECEDING | -- same as:
ROWS BETWEEN n PRECEDING AND CURRENT ROW

Suporte a mecanismos

MotorSupportNotas
SQL Server Sim Supported under this name.
PostgreSQL Sim Supported under this name.
MySQL Sim MySQL 8.0+
SQLite Sim SQLite 3.25+
Oracle Sim Supported under this name.

Exemplos

Any engine

SELECT day, amount,
       SUM(amount) OVER (ORDER BY day ROWS 6 PRECEDING) AS trailing7
FROM daily_sales;

Bom saber

  • The implied end is always CURRENT ROW; you cannot shorten a frame that ends in FOLLOWING or UNBOUNDED FOLLOWING; those need BETWEEN.
  • The shorthand reads cleanly but hides the peer semantics: RANGE UNBOUNDED PRECEDING still includes the current row's peers under RANGE rules.
  • The shorthand is common for running totals (ROWS UNBOUNDED PRECEDING); for anything else, the BETWEEN form tells the reader where the frame ends.

Questions about Frame Shorthand

Is ROWS 6 PRECEDING the same as ROWS BETWEEN 6 PRECEDING AND CURRENT ROW?

Yes, exactly. The BETWEEN form is just spelled out; the shorthand always assumes AND CURRENT ROW.

Why write the long form at all?

Readers and linters can see the end boundary is deliberate, and ports to engines or ORMs that rewrite SQL do not depend on the implicit rule.

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.