How to Fix MySQL Error 4120 (ER_INTERSECT_ALL_MAX_DUPLICATES_EXCEEDED)
You get mysql error 4120 when an intersect all query finds too many duplicate rows. You can fix it by filtering your data early or removing the all keyword. Database engines use internal counters to track matching rows across query blocks. When a counter reaches its hard ceiling, the engine stops the query at once. It returns SQLSTATE HY000 and the symbol ER_INTERSECT_ALL_MAX_DUPLICATES_EXCEEDED. The official message states that the max number of duplicate rows in intersect all exceeded. Knowin
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 4120 when an intersect all query finds too many duplicate rows. You can fix it by filtering your data early or removing the all keyword.
Database engines use internal counters to track matching rows across query blocks. When a counter reaches its hard ceiling, the engine stops the query at once. It returns SQLSTATE HY000 and the symbol ER_INTERSECT_ALL_MAX_DUPLICATES_EXCEEDED. The official message states that the max number of duplicate rows in intersect all exceeded. Knowing why this limit exists helps your team write safer queries in production.
What triggers mysql error 4120 in the database engine
An intersect operation compares two or more result sets and returns shared rows. Standard intersect queries drop duplicate rows from the final output. Intersect all keeps duplicates and must count how many times each row appears. The database engine creates a temporary table in memory or on disk for this task. Each unique row gets a counter to track its match count across sets.
This counter has a fixed maximum size inside the storage engine. If your tables have millions of identical rows, the counter overflows. When the engine hits this limit, it aborts the run and throws mysql error 4120. This often happens when you join tables on low-selectivity columns like status flags or boolean fields.
Bad join conditions in the underlying query blocks also cause this problem. A missing filter can create a cross join by mistake. That cross join explodes the row count before the intersect step even starts. The engine then tries to count billions of identical values and fails.
How to find and fix duplicate row limits in production
Start by checking the query that triggered the failure in your database logs. Look at each select block inside the intersect all statement. Run a count query on each block to see how many duplicate rows exist. You will often find one column with millions of repeated null or default values.
The fastest fix is to switch from intersect all to a standard intersect operation. Most apps only need to know if a value exists in both sets. They do not need millions of identical rows returned to the client. Dropping the all keyword removes the counter overflow risk and speeds up the run.
MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
If your app logic requires duplicate counts, rewrite the query using group by clauses. Count the rows in each table first, then join those grouped results together. You can then pick the smaller count from the two tables in your select list. This gives the exact same result without hitting the internal temporary table limit.
Preventing query failures through safe database practices
You should also add strict where filters to your query blocks before intersecting them. Filter out null keys, archived records, or test rows early in the query plan. Smaller input sets mean fewer duplicates for the engine to track.
Production fixes require care so you do not expose sensitive data during checks. Do not grant full admin roles to developers just to debug a failing query. Use read roles with query masking that rewrites emails and card numbers in results. This lets engineers inspect duplicate row patterns safely while keeping customer data protected.
Check MySQL queries and results on the wire
MySQL grants control who logs in. They do not check single queries or mask sensitive columns once a session starts. With hoop.dev, you run an open-source proxy, a sidecar for your MySQL, inline in the MySQL wire protocol. On every query an AI agent or human runs against production MySQL, RDS, or Aurora, hoop.dev decides to pass, mask, block, or hold it for review.
Multi-statement batches and prepared queries are split and checked one by one by hoop.dev guardrails, which refuse banned queries with a native MySQL error or hold risky ones for approval. On the response path, hoop.dev data masking rewrites string values in returned rows in memory, and every decision is logged. See the MIT-licensed project on GitHub.
Frequently asked questions
What does ER_INTERSECT_ALL_MAX_DUPLICATES_EXCEEDED mean in MySQL?
It means an intersect all query found more identical rows than the internal counter allows. The engine stops the query under SQLSTATE HY000 to prevent corrupt counts.
Does switching to standard intersect fix mysql error 4120?
Yes, removing the all keyword deduplicates rows right away instead of counting every single match. This stops the internal counter from reaching its upper limit.
Can I raise a server setting to allow more duplicate rows?
No, the duplicate row counter limit is built into how the engine tracks set operations. You must fix the issue by rewriting the query or filtering input rows.
Related guides
In this post