Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-11-29 · container image mysql:8.4
ERROR 1242 (21000) appears when a subquery stands for a single value but the data has duplicates. The SQL may have worked for months and fail the day a second row appears (a second address, a second upload).
First find which key has more than one row (GROUP BY ... HAVING COUNT(*) > 1), then decide: pick one row (ORDER BY ... LIMIT 1), or accept many (IN). 3 cases reproduced on MySQL 8.4.
01Subquery in the SELECT list returns several rows for one customer
(SELECT a.city FROM addresses a WHERE a.customer_id = c.id) works while every customer has one address. Customer 1 has two (Seoul, Busan), so the query fails.
Setup
CREATE TABLE customers (id INT PRIMARY KEY, name VARCHAR(20));
CREATE TABLE addresses (id INT PRIMARY KEY, customer_id INT, city VARCHAR(20));
INSERT INTO customers VALUES (1, 'kim'), (2, 'lee');
INSERT INTO addresses VALUES (1, 1, 'Seoul'), (2, 1, 'Busan'), (3, 2, 'Incheon');Query OK, 0 rows affected (0.02 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0SELECT c.name,
(SELECT a.city FROM addresses a WHERE a.customer_id = c.id) AS city
FROM customers c;ERROR 1242 (21000): Subquery returns more than 1 rowSELECT customer_id, COUNT(*) FROM addresses GROUP BY customer_id HAVING COUNT(*) > 1;+-------------+----------+
| customer_id | COUNT(*) |
+-------------+----------+
| 1 | 2 |
+-------------+----------+
1 row in set (0.00 sec)GROUP BY customer_id HAVING COUNT(*) > 1 shows customer 1 with 2 rows. Decide which one you want and say so: ORDER BY a.id LIMIT 1 returned kim → Seoul, lee → Incheon.
SELECT c.name,
(SELECT a.city FROM addresses a WHERE a.customer_id = c.id ORDER BY a.id LIMIT 1) AS city
FROM customers c ORDER BY c.id;+------+---------+
| name | city |
+------+---------+
| kim | Seoul |
| lee | Incheon |
+------+---------+
2 rows in set (0.00 sec)02WHERE col = (SELECT ...) when the subquery returns a list
dept_id = (SELECT id FROM depts WHERE region = 'EAST') expects one department, but EAST has two (10 Sales, 20 Support).
Setup
CREATE TABLE depts (id INT PRIMARY KEY, name VARCHAR(20), region VARCHAR(10));
CREATE TABLE employees (id INT PRIMARY KEY, name VARCHAR(20), dept_id INT);
INSERT INTO depts VALUES (10, 'Sales', 'EAST'), (20, 'Support', 'EAST'), (30, 'Dev', 'WEST');
INSERT INTO employees VALUES (1, 'kim', 10), (2, 'lee', 20), (3, 'park', 30);Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.02 sec)
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0SELECT name FROM employees
WHERE dept_id = (SELECT id FROM depts WHERE region = 'EAST');ERROR 1242 (21000): Subquery returns more than 1 rowSELECT id, name FROM depts WHERE region = 'EAST';+----+---------+
| id | name |
+----+---------+
| 10 | Sales |
| 20 | Support |
+----+---------+
2 rows in set (0.00 sec)Use IN instead of =: WHERE dept_id IN (SELECT id FROM depts WHERE region = 'EAST') returned kim and lee.
SELECT name FROM employees
WHERE dept_id IN (SELECT id FROM depts WHERE region = 'EAST')
ORDER BY id;+------+
| name |
+------+
| kim |
| lee |
+------+
2 rows in set (0.00 sec)03UPDATE ... SET col = (SELECT ...) from a table with duplicate keys
The upload table has two prices for P1 (uploaded on 11-01 and 11-02). SET price = (SELECT u.price ... WHERE u.code = p.code) gets two values for P1 and the whole UPDATE fails.
Setup
CREATE TABLE products (code VARCHAR(10) PRIMARY KEY, price INT);
CREATE TABLE price_upload (code VARCHAR(10), price INT, uploaded_at DATETIME);
INSERT INTO products VALUES ('P1', 1000), ('P2', 2000);
INSERT INTO price_upload VALUES ('P1', 1100, '2026-11-01 09:00:00'), ('P1', 1200, '2026-11-02 09:00:00'), ('P2', 2100, '2026-11-01 09:00:00');Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.02 sec)
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0UPDATE products p
SET price = (SELECT u.price FROM price_upload u WHERE u.code = p.code);ERROR 1242 (21000): Subquery returns more than 1 rowSELECT code, COUNT(*) FROM price_upload GROUP BY code HAVING COUNT(*) > 1;+------+----------+
| code | COUNT(*) |
+------+----------+
| P1 | 2 |
+------+----------+
1 row in set (0.00 sec)Check duplicates with GROUP BY code HAVING COUNT(*) > 1 (P1: 2). Take the latest: ORDER BY u.uploaded_at DESC LIMIT 1. 2 rows changed: P1 1200, P2 2100.
UPDATE products p
SET price = (SELECT u.price FROM price_upload u WHERE u.code = p.code
ORDER BY u.uploaded_at DESC LIMIT 1);
SELECT * FROM products ORDER BY code;Query OK, 2 rows affected (0.01 sec)
Rows matched: 2 Changed: 2 Warnings: 0
+------+-------+
| code | price |
+------+-------+
| P1 | 1200 |
| P2 | 2100 |
+------+-------+
2 rows in set (0.00 sec)FAQ
How do I fix MySQL error 1242 Subquery returns more than 1 row?
Find the duplicated key with GROUP BY ... HAVING COUNT(*) > 1. Then use IN if many rows are fine, or ORDER BY ... LIMIT 1 in the subquery if you need exactly one.
Should I use IN or = with a subquery?
= only works when the subquery returns at most one row. If it can return several, use IN.
My query worked yesterday, why does it fail with 1242 now?
The SQL did not change; the data did. A second row for the same key (here a second address or a second upload) makes the subquery return 2 rows.
How do I pick the latest row in an UPDATE subquery?
Add ORDER BY <date column> DESC LIMIT 1 inside the subquery, as in the price upload example.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary users created for the test, each with full rights on a database of the same name (its default database); root is the administrator. MySQL 8.4.13