MySQL error 3580 occurs when named windows reference each other in a circular pattern. Break the cycle by removing unnecessary windows or restructuring references to form a linear dependency chain.
This error occurs when you define named windows using the WINDOW clause, and those windows reference each other in a way that creates a circular dependency. For example, if window wf2 references wf3, but wf3 also references wf2, MySQL cannot resolve the dependency graph because there is no starting point. Named windows in MySQL can refer to other named windows, but those references must form a valid hierarchy without cycles—similar to how inheritance works in object-oriented programming, where a class cannot inherit from itself directly or indirectly.
Examine your query and list all named windows and what they reference. For example:
SELECT District, Name, Population,
SUM(Population) OVER(wf2),
COUNT(*) OVER(wf3)
FROM City
WINDOW wf1 AS (PARTITION BY District),
wf2 AS (wf3 ORDER BY Name), -- wf2 references wf3
wf3 AS (wf2) -- wf3 references wf2 - CIRCULAR!
ORDER BY District, Name;In this example, wf2 references wf3 and wf3 references wf2, creating a cycle.
Modify your window definitions so each window references an earlier window (or no window), creating a linear dependency chain rather than a cycle. Break the cycle by having one window reference a different window or inherit from the base definition.
SELECT District, Name, Population,
SUM(Population) OVER(wf2),
COUNT(*) OVER(wf3)
FROM City
WINDOW wf1 AS (PARTITION BY District),
wf2 AS (wf1 ORDER BY Name), -- wf2 inherits from wf1
wf3 AS (wf2) -- wf3 inherits from wf2 - NO CYCLE
ORDER BY District, Name;This creates a valid linear dependency: wf1 → wf2 → wf3.
If a named window is not needed, remove it entirely. This is often the simplest solution:
SELECT District, Name, Population,
SUM(Population) OVER(wf2),
COUNT(*) OVER(wf1 ORDER BY Name)
FROM City
WINDOW wf1 AS (PARTITION BY District),
wf2 AS (wf1 ORDER BY Name)
ORDER BY District, Name;Removing wf3 eliminates the circular reference without affecting query functionality.
Run your modified query to verify it executes without the circularity error:
mysql> [your corrected query]If the query executes successfully with the expected results, the circular dependency has been resolved.
MySQL enforces strict rules for named window references: a window can only inherit properties from (reference) a previously defined window. When using the WINDOW clause, think of it as a dependency tree where each node can only depend on nodes that come before it in the declaration order. This is different from some other databases. If you have complex window requirements, consider using subqueries or CTEs (Common Table Expressions) as an alternative to named windows, which can sometimes provide more flexibility. Additionally, remember that when a window inherits from another, you can only add properties like ORDER BY—you cannot modify properties that are already defined in the referenced window (another source of errors like 3581 or 3583).
ERROR 1064: You have an error in your SQL syntax
How to fix "ERROR 1064: You have an error in your SQL syntax" in MySQL
ERROR 1054: Unknown column in field list
Unknown column in field list
ER_WINDOW_RANGE_FRAME_NUMERIC_TYPE (3589): RANGE frame requires numeric ORDER BY expression
RANGE frame requires numeric ORDER BY expression in MySQL window functions
CR_ALREADY_CONNECTED (2058): Handle already connected
How to fix "CR_ALREADY_CONNECTED (2058): Handle already connected" in MySQL
ER_WINDOW_DUPLICATE_NAME (3591): Duplicate window name
How to fix ER_WINDOW_DUPLICATE_NAME (3591) in MySQL window functions