FOLLOWING is a frame-boundary keyword used in the ROWS or RANGE clause inside an OVER() specification for window (analytic) functions. It tells the database how far forward, relative to the current row, the frame should extend. Three forms are common:1. n FOLLOWING – exactly n rows (or range units) after the current row.2. CURRENT ROW – the current row itself.3. UNBOUNDED FOLLOWING – the end of the partition.A window frame always needs a lower and an upper boundary. FOLLOWING can appear only as the upper boundary. The lower boundary is usually n PRECEDING, CURRENT ROW, or UNBOUNDED PRECEDING. If you omit a frame clause, most systems default to RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, which means FOLLOWING is not applied.Because window frames are evaluated per row, FOLLOWING is powerful for forward-looking calculations such as running totals that include future rows, lead-style aggregates, and moving averages. It requires a deterministic ORDER BY inside the window; without ORDER BY, a FOLLOWING frame is undefined and most databases throw an error.Performance considerations: large FOLLOWING offsets or UNBOUNDED FOLLOWING on huge partitions can force the planner to materialize many rows, so indexing the ORDER BY columns is critical. Some engines (e.g., old MySQL versions) support only ROWS, not RANGE, with FOLLOWING.
lower_boundary
(string) - UNBOUNDED PRECEDING|||n PRECEDING|||CURRENT ROWupper_boundary
(string) - n FOLLOWING|||CURRENT ROW|||UNBOUNDED FOLLOWINGn
(non-negative integer) - exact number of rows or range units aheadPRECEDING, UNBOUNDED PRECEDING, CURRENT ROW, WINDOW functions, OVER clause, PARTITION BY
SQL:2003 window function standard
FOLLOWING sets the upper boundary of a window frame so the function can include rows after the current one.
Yes. A common pattern is `ROWS BETWEEN n PRECEDING AND m FOLLOWING`, which gives a centered frame around the current row.
No. If you need only past and current rows, you can omit FOLLOWING and use the default `UNBOUNDED PRECEDING AND CURRENT ROW` frame.
It does in most engines, but MySQL restricts RANGE to `UNBOUNDED FOLLOWING` or `CURRENT ROW` starting in 8.0.12. Check your dialect’s documentation.