SQL Keywords

SQL FOLLOWING

What does the SQL FOLLOWING keyword do?

FOLLOWING sets the upper boundary of a window frame relative to the current row, allowing you to reference rows that come after it.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL FOLLOWING: PostgreSQL, MySQL 8.0+, SQL Server, Oracle, Snowflake, BigQuery, DuckDB, SQLite 3.28+

SQL FOLLOWING Full Explanation

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.

SQL FOLLOWING Syntax

<window_function>() OVER (
  PARTITION BY partition_col1, ...
  ORDER BY order_col
  ROWS BETWEEN <lower_boundary> AND <n | CURRENT ROW | UNBOUNDED> FOLLOWING
);

SQL FOLLOWING Parameters

  • lower_boundary (string) - UNBOUNDED PRECEDING|||n PRECEDING|||CURRENT ROW
  • upper_boundary (string) - n FOLLOWING|||CURRENT ROW|||UNBOUNDED FOLLOWING
  • n (non-negative integer) - exact number of rows or range units ahead

Example Queries Using SQL FOLLOWING

-- Running total that includes the next 3 rows
SELECT order_id,
       amount,
       SUM(amount) OVER (
         ORDER BY order_date
         ROWS BETWEEN CURRENT ROW AND 3 FOLLOWING
       ) AS running_total_next_3
FROM   orders;

-- Moving 7-day average that looks 3 days ahead
SELECT event_date,
       AVG(signups) OVER (
         ORDER BY event_date
         RANGE BETWEEN 3 PRECEDING AND 3 FOLLOWING
       ) AS centered_avg
FROM   daily_signups;

-- Total sales from current row to end of partition
SELECT region,
       month,
       SUM(sales) OVER (
         PARTITION BY region
         ORDER BY month
         ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
       ) AS future_sales
FROM   monthly_sales;

Expected Output Using SQL FOLLOWING

  • Each query returns the original rows plus an extra column whose value is computed over the specified forward-looking frame
  • No rows are added or removed

Use Cases with SQL FOLLOWING

  • Calculating totals that include future rows (e.g., forecast remaining revenue)
  • Centered moving averages that require forward and backward rows
  • Determining cumulative counts up to the end of a partition
  • Comparing the current row to values a fixed number of rows ahead

Common Mistakes with SQL FOLLOWING

  • Omitting ORDER BY inside OVER() – leads to errors or nondeterministic results
  • Using FOLLOWING as the lower boundary – invalid syntax
  • Supplying a negative offset (e.g., -1 FOLLOWING) – not allowed
  • Forgetting that UNBOUNDED FOLLOWING with large partitions can hurt performance

Related Topics

PRECEDING, UNBOUNDED PRECEDING, CURRENT ROW, WINDOW functions, OVER clause, PARTITION BY

First Introduced In

SQL:2003 window function standard

Frequently Asked Questions

What is SQL FOLLOWING?

FOLLOWING sets the upper boundary of a window frame so the function can include rows after the current one.

Can I mix PRECEDING and FOLLOWING in the same frame?

Yes. A common pattern is `ROWS BETWEEN n PRECEDING AND m FOLLOWING`, which gives a centered frame around the current row.

Is FOLLOWING required in window functions?

No. If you need only past and current rows, you can omit FOLLOWING and use the default `UNBOUNDED PRECEDING AND CURRENT ROW` frame.

Does FOLLOWING work with RANGE frames?

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.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!