3187
MySQLERRORNotableWindow FunctionsHIGH confidence

Window ORDER BY mismatch with named window

Production Risk

Low — caught at validation time.

Why it happens
  1. 1A window function references a named window and adds conflicting ORDER BY expressions.
  2. 2The referencing window adds ORDER BY when the referenced named window already has one.
How to reproduce
trigger — this will error
trigger — this will error
SELECT SUM(val) OVER (w ORDER BY id) FROM t1 WINDOW w AS (ORDER BY name);

expected output

ERROR 3187 (HY000): Cannot override ORDER BY of window.

Fix

Remove duplicate ORDER BY from the inline window reference

Remove duplicate ORDER BY from the inline window reference
SELECT SUM(val) OVER (w) FROM t1 WINDOW w AS (ORDER BY id);

Why this works

Named windows carry their ORDER BY; inline references must not override it.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 3187 ER_WINDOW_WRONG_ORDER

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MySQL errors