How to fix ER_WINDOW_DUPLICATE_NAME (3591): Duplicate window name in MySQL

MySQLBEGINNERMEDIUMUpdated Dec 29, 2025ยท 93 views

MySQL throws ER_WINDOW_DUPLICATE_NAME when the WINDOW clause in a SELECT statement defines the same window name more than once. Each named window specification must have a unique identifier to avoid ambiguity when functions reference them.

Quick fix

Fixes 3591

SELECT col1, SUM(col2) OVER w1, AVG(col2) OVER w2
FROM table_name
WINDOW w1 AS (PARTITION BY col1 ORDER BY col2),
       w2 AS (PARTITION BY col1 ORDER BY col3);
  1. Identify all window names in the WINDOW clause
  2. Rename the duplicate window to a unique identifier
  3. Remove the duplicate definition if windows are identical
  4. Verify OVER clause references match window names
mysql โ€” error
SELECT col1, SUM(col2) OVER w1, AVG(col2) OVER w2
FROM table_name
WINDOW w1 AS (PARTITION BY col1 ORDER BY col2),
w2 AS (PARTITION BY col1 ORDER BY col3);
ER_WINDOW_DUPLICATE_NAME (3591): Duplicate window name

What this error means

When using named windows in MySQL, you define a reusable window specification in the WINDOW clause with a unique name. Each name must appear exactly once in the WINDOW clause. If you use the same window name twice in the WINDOW clause, MySQL raises ER_WINDOW_DUPLICATE_NAME because the parser cannot determine which definition to use when the window is referenced by name in an OVER clause. The error occurs at parse time, before the query executes. This is different from reusing the same window name across multiple OVER clauses (which is allowed) or modifying a named window with additional clauses in the OVER clause (which is also allowed). The restriction is specifically about duplicate definitions in the WINDOW clause itself. ```sql -- Error: window 'w' is defined twice SELECT id, SUM(value) OVER w AS sum1, AVG(value) OVER w AS sum2 FROM metrics WINDOW w AS (PARTITION BY account_id ORDER BY created_at), w AS (ORDER BY created_at); -- ERROR 3591: duplicate name 'w' -- Fixed: each window has a unique name SELECT id, SUM(value) OVER w1 AS sum1, AVG(value) OVER w2 AS sum2 FROM metrics WINDOW w1 AS (PARTITION BY account_id ORDER BY created_at), w2 AS (ORDER BY created_at); ```

Typical symptoms

  • Query fails with ER_WINDOW_DUPLICATE_NAME (3591) at parse time
  • Multiple WINDOW clauses with the same name in the same SELECT statement
  • Error message explicitly names the duplicate window identifier
  • The query never executes because the syntax error is caught during parsing

Common causes

  1. You accidentally defined the same window name in the WINDOW clause twice (copy-paste or editing error)
  2. You renamed one window definition but forgot to rename the other, creating a duplicate
  3. When refactoring a query with multiple named windows, duplicate definitions were introduced unintentionally
  4. Merging two queries that each define a window with the same name without renaming one

How to fix "ER_WINDOW_DUPLICATE_NAME (3591): Duplicate window name"

1Identify all window names in the WINDOW clause

Scan your WINDOW clause for all window names. Example:

sql
SELECT col1, SUM(col2) OVER w1, AVG(col2) OVER w2
FROM table_name
WINDOW w1 AS (PARTITION BY col1 ORDER BY col2),
       w2 AS (PARTITION BY col1 ORDER BY col3);

Check that each name (w1, w2, etc.) appears exactly once. If you see a name listed more than once, that is the problem.

2Rename the duplicate window to a unique identifier

Choose a descriptive new name for the duplicate window and update both the WINDOW clause definition and any OVER clauses that reference it.

sql
-- Before: 'w' is defined twice
SELECT col1, SUM(col2) OVER w, AVG(col2) OVER w
FROM table_name
WINDOW w AS (PARTITION BY col1 ORDER BY col2),
       w AS (ORDER BY col3);

-- After: second 'w' renamed to 'w_unordered'
SELECT col1, SUM(col2) OVER w, AVG(col2) OVER w_unordered
FROM table_name
WINDOW w AS (PARTITION BY col1 ORDER BY col2),
       w_unordered AS (ORDER BY col3);

Make sure the new name is used consistently in all OVER clauses that reference it.

3Remove the duplicate definition if windows are identical

If both window definitions are identical (same PARTITION BY, ORDER BY, and frame specification), you only need one. Delete the duplicate and use the single window name in all relevant OVER clauses.

sql
-- Before: 'w' is defined twice with identical specs
SELECT col1, SUM(col2) OVER w, AVG(col2) OVER w
FROM table_name
WINDOW w AS (PARTITION BY col1 ORDER BY col2),
       w AS (PARTITION BY col1 ORDER BY col2);

-- After: keep only one definition
SELECT col1, SUM(col2) OVER w, AVG(col2) OVER w
FROM table_name
WINDOW w AS (PARTITION BY col1 ORDER BY col2);

This simplifies the query and eliminates the ambiguity.

4Verify OVER clause references match window names

After renaming or removing duplicates, double-check that every OVER clause that references a named window uses the correct (non-duplicate) name.

sql
SELECT
  ROW_NUMBER() OVER w1 AS rn,
  SUM(amount) OVER w2 AS running_total
FROM sales
WINDOW w1 AS (ORDER BY sale_date),
       w2 AS (PARTITION BY customer_id ORDER BY sale_date);

If a name in an OVER clause does not match any name in the WINDOW clause, you will get ER_WINDOW_NO_SUCH_WINDOW instead. Ensure all references are consistent.

Advanced notes

MySQL allows you to reuse the same window name across multiple OVER clauses and even to inherit from one named window to another (for example, WINDOW w2 AS (w1 ORDER BY col) inherits w1's definition and adds ordering). The uniqueness constraint applies only to the definitions in the WINDOW clause itself, not to their usage. If you need multiple similar windows with subtle differences (different partitioning or ordering), give each a distinct name rather than trying to redefine the same one.

Frequently asked questions

What does "3591" mean?
When using named windows in MySQL, you define a reusable window specification in the WINDOW clause with a unique name. Each name must appear exactly once in the WINDOW clause. If you use the same window name twice in the WINDOW clause, MySQL raises ER_WINDOW_DUPLICATE_NAME because the parser cannot determine which definition to use when the window is referenced by name in an OVER clause.
How do I fix "3591"?
MySQL throws ER_WINDOW_DUPLICATE_NAME when the WINDOW clause in a SELECT statement defines the same window name more than once. Each named window specification must have a unique identifier to avoid ambiguity when functions reference them.
What causes "3591"?
You accidentally defined the same window name in the WINDOW clause twice (copy-paste or editing error) You renamed one window definition but forgot to rename the other, creating a duplicate When refactoring a query with multiple named windows, duplicate definitions were introduced unintentionally
Is the "3591" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading