3172
MySQLWARNINGCriticalWindow FunctionsMEDIUM confidence

Window specification ignores GROUP BY / ORDER BY from outer query

Production Risk

Medium — warning indicates potentially incorrect query logic.

What this means

A window function is used in a query that has a GROUP BY or ORDER BY, but the window specification does not reference these grouping or ordering columns. This may produce unexpected results.

Why it happens
  1. 1Window function defined without PARTITION BY or ORDER BY while the outer query uses GROUP BY.
How to reproduce
trigger — this will error
trigger — this will error
SELECT dept, SUM(sal) OVER () FROM t1 GROUP BY dept;

expected output

Warning (Code 3172): Window specification ignores outer GROUP BY.

Fix

Add appropriate PARTITION BY to the window

Add appropriate PARTITION BY to the window
SELECT dept, SUM(sal) OVER (PARTITION BY dept) FROM t1;

Why this works

The window operates independently of GROUP BY; define the window explicitly.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 3172 ER_WINDOW_NO_GROUP_ORDER_UNUSED

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

← All MySQL errors