How to Fix MySQL Error 3575 (ER_CTE_RECURSIVE_FORBIDS_AGGREGATION)
You trigger mysql error 3575 when a recursive query block contains an aggregate or window function. To fix it, move the aggregation or window logic outside the recursive step. MySQL uses the symbol ER_CTE_RECURSIVE_FORBIDS_AGGREGATION and SQLSTATE HY000 for this failure. The database engine blocks these functions inside the loop so each step stays predictable. When developers try to sum values or rank rows mid-loop, the query fails right away. This guide explains why the engine enforces this ru
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You trigger mysql error 3575 when a recursive query block contains an aggregate or window function. To fix it, move the aggregation or window logic outside the recursive step.
MySQL uses the symbol ER_CTE_RECURSIVE_FORBIDS_AGGREGATION and SQLSTATE HY000 for this failure. The database engine blocks these functions inside the loop so each step stays predictable. When developers try to sum values or rank rows mid-loop, the query fails right away. This guide explains why the engine enforces this rule and how to rewrite your query.
What causes mysql error 3575 in the database engine
A recursive common table expression has two distinct parts. The first part is the seed query, which builds the initial row set. The second part is the recursive query block, which references the expression itself. Each pass of the recursive block should only inspect the rows from the prior pass.
The official message states that the recursive block forbids both aggregation and window functions. Aggregate functions like sums, counts, and averages need the full set of rows to work. Window functions also need a complete partition of rows to rank or compare values. During recursion, the full row set does not exist yet.
Because the table is still growing, MySQL blocks group operations inside the recursive member. If the engine allowed them, results would change based on internal processing order. You can still use aggregates inside the initial seed query block without any issue. The restriction applies only to the lower query block that loops over the table.
How to diagnose and fix the query in production
Start by locating the recursive expression named in the error message. Look at the query block that comes after the union keyword. Check its select list and clauses for group functions or window clauses. Common culprits include counting child nodes, summing tree weights, or ranking hierarchy levels inside the loop.
To fix the query, strip all grouping and window logic from the recursive block. Instead, pass the raw values through each step of the recursion. You can track depth or running totals by adding simple math to a column. For example, add one to a level column on each pass of the loop.
MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Once the recursive expression finishes building the full tree, run your math in the outer query. The outer select statement sees the final result set after the loop stops. There, you can group rows, sum totals, and apply window functions without hitting engine limits. This keeps the recursive step fast and avoids full table scans mid-loop.
How to test recursive query fixes safely
Rewriting recursive queries in a live system carries real risk. When you remove a group clause from a loop, the query might return many more rows. A small mistake in the join condition can even create an infinite loop. Never test raw recursive changes directly against a busy production database with broad write roles.
First, test the updated query using a read-only database role on a staging copy. Make sure your session enforces a sensible limit on recursive passes and query run time. Check the execution plan to confirm the seed and recursive blocks use index lookups. If the outer query processes user records, ensure data masking rewrites emails and phone numbers.
After the query passes review in staging, run it in production with a least-privilege role. Keep an eye on temporary table memory usage when the outer aggregation runs. Separating the recursion from the final summary keeps your database stable and easy to debug.
Inspect every MySQL query and response in flight
Static grants and network rules block bad logins. They cannot pause a risky query for review or mask emails in a result set. By design, hoop.dev sits inline in the MySQL protocol as a sidecar for your MySQL. For every plain query, prepared statement, or cursor an agent or human runs in production, hoop.dev decides to pass, mask, block, or hold it for review.
First, hoop.dev guardrails block banned actions before MySQL runs them, and an AI Analyzer you configure can score intent or escalate for approval. Next, hoop.dev data masking rewrites sensitive string columns in results in memory, and every verdict goes to the audit trail. Find the open-source binary on GitHub.
Frequently asked questions
Can I use aggregate functions in the non-recursive seed block?
Yes, the seed query runs once before the recursion starts. You can use group functions and window logic in the seed block without triggering error 3575.
How do I calculate a running total without sum in a recursive CTE?
Carry a numeric column from the seed query into the recursive block. In the recursive select list, add the new row value to the carried column value.
Does error 3575 also block distinct in the recursive query block?
No, error 3575 specifically targets aggregate and window functions. You can use a distinct union to remove duplicate rows between recursive passes.
Related guides
In this post