3162
MariaDBERRORNotableWindow FunctionsHIGH confidence

Window function cannot redefine ORDER BY from named window

Production Risk

Low — syntax error; restructure the window definition.

What this means

A window function references a named window defined in the WINDOW clause, but the function definition also specifies an ORDER BY clause, which is not allowed — the ORDER BY must come from the named window or the function, not both.

Why it happens
  1. 1Using a named window that already has ORDER BY while also specifying ORDER BY in the window function reference.
How to reproduce
trigger — this will error
trigger — this will error
SELECT SUM(val) OVER (w ORDER BY id) FROM t1 WINDOW w AS (PARTITION BY cat ORDER BY id);

expected output

ERROR 3162 (HY000): Named window cannot be redefined.

Fix 1

Remove ORDER BY from the function reference

Remove ORDER BY from the function reference
SELECT SUM(val) OVER w FROM t1 WINDOW w AS (PARTITION BY cat ORDER BY id);

Why this works

The named window provides the full definition; no override needed.

Fix 2

Define a new named window without ORDER BY and add it in the reference

Define a new named window without ORDER BY and add it in the reference
SELECT SUM(val) OVER (w ORDER BY id) FROM t1 WINDOW w AS (PARTITION BY cat);

Why this works

Base window has no ORDER BY, allowing the function to specify it.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 3162 ER_WINDOW_NO_REDEFINE_ORDER_BY

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

← All MariaDB errors