How to Fix MySQL Error 4114 (ER_CTE_RECURSIVE_NOT_UNION)
You get mysql error 4114 when a recursive query uses EXCEPT or INTERSECT operators. You fix it by rewriting the query to use UNION or UNION ALL instead. Database teams often use recursive Common Table Expressions to query hierarchical data. Tree structures like company charts or bill of materials rely on these loops. Still, the database engine puts strict rules on how these loops build result sets. When your query breaks these rules, the engine stops the run right away. It returns symbol ER_CTE
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 get mysql error 4114 when a recursive query uses EXCEPT or INTERSECT operators. You fix it by rewriting the query to use UNION or UNION ALL instead.
Database teams often use recursive Common Table Expressions to query hierarchical data. Tree structures like company charts or bill of materials rely on these loops. Still, the database engine puts strict rules on how these loops build result sets. When your query breaks these rules, the engine stops the run right away. It returns symbol ER_CTE_RECURSIVE_NOT_UNION under SQLSTATE HY000.
Why mysql error 4114 happens in recursive queries
The official message states that a recursive table reference in EXCEPT or INTERSECT is not allowed. A recursive Common Table Expression has two main parts. The first part is the seed query, which sets up the starting rows. The second part is the recursive member, which references the table name to loop.
In MySQL, you must join the seed query and the recursive part with UNION. You can use either UNION or UNION ALL to combine these two blocks. If you put EXCEPT or INTERSECT between the seed and recursive blocks, the parser fails. The engine also fails if you use those operators inside the recursive part itself.
This rule exists because recursive loops need to add rows in a predictable way. Set difference and set intersection change how past rows relate to new rows. If a loop removes rows from earlier steps, the engine cannot find a clear end. To stop infinite loops and broken state, MySQL blocks these operators during the loop.
How to diagnose and fix the query safely
First, find the exact query that caused the error in your app logs. Look for the recursive clause at the top of the query statement. Check the operators that sit between each select block inside that recursive definition. You will see an EXCEPT or INTERSECT keyword near the recursive table name.
To fix the problem, move the set operator outside the recursive table definition. Change the recursive query so it only uses UNION or UNION ALL to gather rows. Let the loop finish building the full tree or graph of data first.
MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Next, apply your filter in the outer query or in a second non-recursive expression. If you wanted to exclude specific rows with EXCEPT, use a left join instead. You can also filter rows out with a standard WHERE clause in the final select. If you wanted to find matching rows with INTERSECT, use an inner join outside the loop.
Safe ways to test query fixes in production
Rewriting recursive queries requires care because bad loop logic can lock up a server. When you remove an intersection, the loop might pull back many more rows than before. Always test your updated query in a staging database before running it in production. Check that the base case and the stop condition in the loop still work.
Do not raise global memory limits or turn off loop counters to force a run. Keep the default recursion depth rules in place to protect the database host. When engineers debug live data, make sure sensitive fields like emails or card numbers stay safe. Good data masking rewrites matched fields in results so tests do not expose private records. Once the new UNION query returns the right rows fast, deploy the code update.
Add wire guardrails and masking beside MySQL
Database grants and TLS setup decide who can reach MySQL. Even so, an open session can run broad updates or read raw personal data from allowed tables. In practice, hoop.dev sits inline in the wire protocol beside the database as a sidecar for your MySQL. On every production query an agent or engineer sends, hoop.dev decides to pass, mask, block, or escalate it for human review.
With hoop.dev MySQL support, guardrails refuse bad queries before they run or hold risky calls for approval, while data masking rewrites matched sensitive values in result rows in memory. Furthermore, hoop.dev holds SOC 2 Type II and generates evidence for GDPR, HIPAA, and PCI DSS programs. Read the docs and source on GitHub.
Frequently asked questions
What does ER_CTE_RECURSIVE_NOT_UNION mean in MySQL?
It means a recursive query tried to use EXCEPT or INTERSECT alongside its self-reference. MySQL requires you to use UNION or UNION ALL for the recursive step.
Can I use EXCEPT or INTERSECT anywhere in a query with a recursive CTE?
Yes, you can use them in the seed block or in the final outer query. You just cannot use them to connect or filter the recursive table reference itself.
How do I replace INTERSECT inside a recursive query?
Rewrite the logic using an INNER JOIN or an EXISTS subquery inside the recursive select block. Keep the main link between the seed and the recursive step as a UNION.
Related guides
In this post