3266
MySQLERRORNotableWindow FunctionsHIGH confidence
Window frame with RANGE requires ORDER BY with exactly one key
Production Risk
Low — query is rejected; use ROWS or fix the ORDER BY.
How to reproduce
trigger — this will error
trigger — this will error
SELECT SUM(amount) OVER (ORDER BY year, month RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM sales;
expected output
ERROR 3266 (HY000): Window 'w': RANGE-type frame requires ORDER BY with exactly one expression.
Fix 1
Switch to ROWS frame
Switch to ROWS frame
SELECT SUM(amount) OVER (ORDER BY year, month ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM sales;
Why this works
ROWS frames do not require a single ORDER BY key.
Fix 2
Use a single ORDER BY key
Use a single ORDER BY key
SELECT SUM(amount) OVER (ORDER BY sale_date RANGE BETWEEN INTERVAL 30 DAY PRECEDING AND CURRENT ROW) FROM sales;
Why this works
A single date/numeric ORDER BY expression allows RANGE frames with offsets.
What not to do
✕
Sources
Official documentation ↗
MySQL 8.0 — 3266 ER_WINDOW_FRAME_OF_NON_CANONICAL_RANGE
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev