How to Fix MySQL Error 1242 (ER_SUBQUERY_NO_1_ROW)
A mysql error 1242 happens when a nested query returns more than one row. You can fix it by limiting the inner query or changing the comparison logic. This failure uses the official symbol ER_SUBQUERY_NO_1_ROW and maps to SQLSTATE 21000. The database engine stops the query to prevent wrong or unexpected data changes. It occurs when an outer query expects a single value but gets a list. Teams often see this bug when new data breaks old assumptions about unique rows. What causes mysql error 124
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.
A mysql error 1242 happens when a nested query returns more than one row. You can fix it by limiting the inner query or changing the comparison logic.
This failure uses the official symbol ER_SUBQUERY_NO_1_ROW and maps to SQLSTATE 21000. The database engine stops the query to prevent wrong or unexpected data changes. It occurs when an outer query expects a single value but gets a list. Teams often see this bug when new data breaks old assumptions about unique rows.
What causes mysql error 1242 in the database engine
The database engine throws this error when a scalar subquery returns multiple rows. A scalar subquery is an inner query that must return one single value. You might place this inner query inside a select list or a where clause. You might also use it inside a set clause during an update.
When you use basic math or equality operators, the engine expects one value. If the inner table holds duplicate records, the subquery sends back multiple rows. The engine cannot compare one single column value against a list of values. It halts the work right away and returns the message that the subquery failed.
This issue also happens inside triggers and stored routines during data writes. An app might run fine for months with clean, unique data in every table. Then a batch job inserts two rows with the same lookup key. The next time the trigger runs, the inner query finds both rows and fails.
How to find the failing subquery in production
To fix the bug, you first need to find the exact inner query. Check your app error logs to find the full statement that failed. If the query has many nested parts, test each inner query on its own. Run each inner query as a separate read statement using the same filter values.
Count how many rows each inner query returns when you run it alone. Any inner query that returns two or more rows is the root cause. Look at the table data to see why multiple rows match your filter. You will often find missing unique rules or missing filter conditions on joined tables.
MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
When you check production data, keep strict data privacy rules in mind. Masking rewrites matched fields in results, such as emails, phone numbers, and card numbers. This lets your team inspect duplicate rows safe from raw sensitive data exposure. Never grant broad write roles just to debug a broken read or update query.
How to fix the subquery and prevent future errors
You have four main ways to fix a subquery that returns too many rows. First, you can change the equality operator to an inclusion operator in your filter. This lets the outer query match against a full list of returned values. This works well when the inner query sits inside a where clause.
Second, you can add an aggregate function to the inner query select list. Functions that find the minimum, maximum, or average value always return one single row. Make sure this math logic matches what your business rules actually need.
Third, you can add a sort order and a single row limit. This forces the inner query to pick the top match and drop the rest. Always include a clear sort order so the database picks the right row.
Fourth, you can rewrite the query to use a standard table join instead. Joins often make duplicate row matches much easier to see and group. Finally, add a unique key rule to the table if duplicates should never exist.
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
What does SQLSTATE 21000 mean with ER_SUBQUERY_NO_1_ROW?
SQLSTATE 21000 is the standard code for a cardinality violation in SQL databases. In MySQL, it pairs with code 1242 when a scalar subquery returns multiple rows. It means the shape of the returned data does not match the operator.
Why did my query work in staging but fail with error 1242 in production?
Staging databases often have smaller, cleaner data sets without duplicate lookup records. Production tables collect duplicate entries over time if they lack a unique key rule. When the inner query hits those extra rows in production, the query fails.
Can an update or delete statement trigger mysql error 1242?
Yes, update and delete statements often trigger this exact error in production apps. If a subquery in the set or where clause returns two rows, it fails. Even a select query can fail if a subquery sits in the column list.
Related guides
In this post