SQL frame shorthand syntax
Writing just a start boundary (ROWS UNBOUNDED PRECEDING) omits BETWEEN, and the frame then ends at CURRENT ROW by definition.
- 类别
- 窗口
- Returns
frame specification- In this category
- 55 of 79
语法
ROWS n PRECEDING | RANGE UNBOUNDED PRECEDING | -- same as: ROWS BETWEEN n PRECEDING AND CURRENT ROW
引擎支持
| 引擎 | Support | 备注 |
|---|---|---|
| SQL Server | 是 | Supported under this name. |
| PostgreSQL | 是 | Supported under this name. |
| MySQL | 是 | MySQL 8.0+ |
| SQLite | 是 | SQLite 3.25+ |
| Oracle | 是 | Supported under this name. |
示例
Any engine
SELECT day, amount,
SUM(amount) OVER (ORDER BY day ROWS 6 PRECEDING) AS trailing7
FROM daily_sales;
值得了解
- 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.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
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.