3284
MySQLERRORNotableWindow FunctionsHIGH confidence

Invalid use of window function alias

Production Risk

Low — query is rejected; restructure with a subquery.

How to reproduce
trigger — this will error
trigger — this will error
SELECT ROW_NUMBER() OVER (ORDER BY id) AS rn FROM t1 WHERE rn < 10;

expected output

ERROR 3284 (HY000): Window function 'rn' cannot be referenced in WHERE clause.

Fix

Use a derived table

Use a derived table
SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY id) AS rn, id FROM t1) sub WHERE rn < 10;

Why this works

The outer query can filter on the window function alias computed in the inner query.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 3284 ER_INVALID_WINDOW_FUNC_ALIAS_USE

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

← All MySQL errors