Mastering MySQL for Data AnalystsLesson 33 of 42
Lesson 33 of 42

Window Frames: ROWS BETWEEN, UNBOUNDED PRECEDING & CURRENT ROW in MySQL

Overview & Learning Objectives

While `PARTITION BY` divides a table into broad categories, the Window Frame specification defines the precise subset of rows within that partition evaluated for each individual calculation. In MySQL 8.0, mastering window frame boundaries (`UNBOUNDED PRECEDING`, `n PRECEDING`, `CURRENT ROW`, `n FOLLOWING`, `UNBOUNDED FOLLOWING`) provides surgical control over rolling metrics, centered moving averages, and cumulative distributions.

  • Master the syntax of window frame clauses (`ROWS BETWEEN ... AND ...`).
  • Distinguish physical row frames (`ROWS`) from logical value frames (`RANGE`).
  • Build centered 3-period moving averages and forward-looking cumulative projections.

Industry Standard

Standard: Algorithmic trading desks at Citadel and quantitative funds use centered window frames (`ROWS BETWEEN 3 PRECEDING AND 3 FOLLOWING`) to filter high-frequency noise from market tick data.

Real-World Example: ScoopCraft Dark-Store Centered Demand Smoothing

ScoopCraft applies a 3-day centered moving average (yesterday, today, tomorrow) to smooth out delivery fluctuations.

DateDemand UnitsWindow Frame DefinitionComputed Output
Nov 01120 unitsROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW120 units (Cumulative Day 1)
Nov 02180 unitsROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING150 units (Avg of Nov 1, 2, 3)
Nov 03150 unitsROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING180 units (Avg of Nov 2, 3, 4)
Nov 04210 unitsROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING165 units (Entire Month Avg)

Syntax & Core SQL Implementation

-- Precise Window Frame specifications in MySQL 8.0
SELECT 
    order_date,
    daily_sales,
    -- 1. Full Partition Total (Entire dataset)
    SUM(daily_sales) OVER (
        ORDER BY order_date
        ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
    ) AS entire_period_total,
    -- 2. Standard Cumulative Total
    SUM(daily_sales) OVER (
        ORDER BY order_date
        ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
    ) AS cumulative_running_total,
    -- 3. Centered 3-Day Moving Average
    ROUND(AVG(daily_sales) OVER (
        ORDER BY order_date
        ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING
    ), 2) AS centered_3day_avg,
    -- 4. Forward-looking 3-Day Projection (Today + next 2 days)
    SUM(daily_sales) OVER (
        ORDER BY order_date
        ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING
    ) AS forward_3day_sum
FROM daily_sales;

Parameters & Arguments Breakdown

  • UNBOUNDED PRECEDING: Frame boundary starting at the very first row of the partition.
  • n PRECEDING: Frame boundary starting n physical rows before the current row.
  • CURRENT ROW: Frame boundary anchored at the active candidate row being processed.
  • n FOLLOWING: Frame boundary extending n physical rows after the current row.
  • UNBOUNDED FOLLOWING: Frame boundary extending to the very last row of the partition.

Sample Walkthrough (Q&A)

Question: What is the danger of using RANGE without specifying frame bounds when ordering by a column with duplicate values?

SQL Pattern: SUM(sales) OVER (ORDER BY date_col)

Result: All rows sharing the identical date_col value receive the combined sum of all peers at once

Explanation: RANGE treats all rows with identical order key values as peers, expanding the frame to include all peers simultaneously. ROWS prevents this.

Common Mistakes & Limitations

  • Specifying a start frame boundary that is lower than the end boundary (e.g. `ROWS BETWEEN CURRENT ROW AND 1 PRECEDING`), triggering a syntax error.
  • Assuming `RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING` works with string sorting columns in MySQL.

Keyboard Shortcuts (MySQL Workbench)

ActionWindowsMac
Execute QueryCtrl + EnterCmd + Enter

Pro Tips & Modern Best Practices

  • Always default to `ROWS` rather than `RANGE` unless you specifically require peer-group aggregation.
  • Named windows (`WINDOW w AS (...)`) clean up verbose queries when multiple calculations share the exact same framing bounds.