MySQL 8.4Lab-verified · 2026-11-29

MySQL ERROR 1242 (21000): Subquery returns more than 1 row

A subquery used where one value is expected (SELECT list, =, SET) returned two or more rows.

MySQL 8.4.13
1242 (21000): Subquery returns more than 1 row

Jump to a cause

  1. 01Subquery in the SELECT list returns several rows for one customer
  2. 02WHERE col = (SELECT ...) when the subquery returns a list
  3. 03UPDATE ... SET col = (SELECT ...) from a table with duplicate keys

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
Setuprun as lab_a
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');
OUTPUT
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: 0
Reproducerun as lab_a
SELECT c.name,
  (SELECT a.city FROM addresses a WHERE a.customer_id = c.id) AS city
FROM customers c;
✕ ERROR
ERROR 1242 (21000): Subquery returns more than 1 row
Checkrun as lab_a
SELECT customer_id, COUNT(*) FROM addresses GROUP BY customer_id HAVING COUNT(*) > 1;
OUTPUT
+-------------+----------+
| 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.

After the fixrun as lab_a
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;
✓ SUCCESS
+------+---------+
| 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
Setuprun as lab_a
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);
OUTPUT
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: 0
Reproducerun as lab_a
SELECT name FROM employees
WHERE dept_id = (SELECT id FROM depts WHERE region = 'EAST');
✕ ERROR
ERROR 1242 (21000): Subquery returns more than 1 row
Checkrun as lab_a
SELECT id, name FROM depts WHERE region = 'EAST';
OUTPUT
+----+---------+
| 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.

After the fixrun as lab_a
SELECT name FROM employees
WHERE dept_id IN (SELECT id FROM depts WHERE region = 'EAST')
ORDER BY id;
✓ SUCCESS
+------+
| 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
Setuprun as lab_a
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');
OUTPUT
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: 0
Reproducerun as lab_a
UPDATE products p
SET price = (SELECT u.price FROM price_upload u WHERE u.code = p.code);
✕ ERROR
ERROR 1242 (21000): Subquery returns more than 1 row
Checkrun as lab_a
SELECT code, COUNT(*) FROM price_upload GROUP BY code HAVING COUNT(*) > 1;
OUTPUT
+------+----------+
| 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.

After the fixrun as lab_a
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;
✓ SUCCESS
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