MySQL 8.4Lab-verified · 2026-12-01

MySQL ERROR 1093 (HY000): You can't specify target table for update in FROM clause

An UPDATE, DELETE or INSERT reads the same table it is changing inside a subquery, which MySQL does not allow.

MySQL 8.4.13
1093 (HY000): You can't specify target table 'products' for update in FROM clause

Jump to a cause

  1. 01UPDATE ... WHERE id IN (SELECT id FROM the same table)
  2. 02Deleting duplicates with NOT IN (SELECT MIN(id) ... GROUP BY)
  3. 03SET column = (SELECT AVG(...) FROM the same table)
  4. 04INSERT ... VALUES ((SELECT MAX(no) + 1 FROM the same table), ...)

Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-12-01 · container image mysql:8.4

MySQL refuses an UPDATE, DELETE or INSERT ... VALUES whose subquery reads the table being modified. The message names the table, or the alias if you used one ('e' in the third case).

The reliable fix is to move the subquery into a JOIN with a derived table (JOIN (SELECT ...) AS t) or to join the table to itself (for INSERT, use INSERT ... SELECT). Below are four common cases verified on MySQL 8.4.

01UPDATE ... WHERE id IN (SELECT id FROM the same table)

The goal is a 10% discount on products with stock over 100. The subquery reads products, the same table the UPDATE changes, so MySQL returns 1093.

Setup
Setuprun as lab_a
CREATE TABLE products (id INT PRIMARY KEY, category VARCHAR(20), price INT, stock INT);
INSERT INTO products VALUES (1,'pen',1000,150),(2,'pen',1200,20),(3,'note',3000,300),(4,'note',2500,5);
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
UPDATE products SET price = price * 0.9
WHERE id IN (SELECT id FROM products WHERE stock > 100);
✕ ERROR
ERROR 1093 (HY000): You can't specify target table 'products' for update in FROM clause
Checkrun as lab_a
SELECT id, stock FROM products WHERE stock > 100;
OUTPUT
+----+-------+
| id | stock |
+----+-------+
|  1 |   150 |
|  3 |   300 |
+----+-------+
2 rows in set (0.00 sec)

The subquery alone returns ids 1 and 3. Join it as a derived table: UPDATE products p JOIN (SELECT id FROM products WHERE stock > 100) AS t ON t.id = p.id SET p.price = p.price * 0.9. 2 rows change: 1000 to 900 and 3000 to 2700.

After the fixrun as lab_a
UPDATE products p
JOIN (SELECT id FROM products WHERE stock > 100) AS t ON t.id = p.id
SET p.price = p.price * 0.9;
SELECT id, price, stock FROM products ORDER BY id;
✓ SUCCESS
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

+----+-------+-------+
| id | price | stock |
+----+-------+-------+
|  1 |   900 |   150 |
|  2 |  1200 |    20 |
|  3 |  2700 |   300 |
|  4 |  2500 |     5 |
+----+-------+-------+
4 rows in set (0.00 sec)

02Deleting duplicates with NOT IN (SELECT MIN(id) ... GROUP BY)

To keep one row per email, DELETE ... WHERE id NOT IN (SELECT MIN(id) FROM members GROUP BY email) is the usual first attempt. It reads members while deleting from it, so 1093.

Setup
Setuprun as lab_a
CREATE TABLE members (id INT PRIMARY KEY, email VARCHAR(50));
INSERT INTO members VALUES (1,'a@x.com'),(2,'b@x.com'),(3,'a@x.com'),(4,'a@x.com'),(5,'b@x.com');
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
DELETE FROM members
WHERE id NOT IN (SELECT MIN(id) FROM members GROUP BY email);
✕ ERROR
ERROR 1093 (HY000): You can't specify target table 'members' for update in FROM clause
Checkrun as lab_a
SELECT email, COUNT(*) AS cnt, MIN(id) AS keep_id FROM members GROUP BY email;
OUTPUT
+---------+-----+---------+
| email   | cnt | keep_id |
+---------+-----+---------+
| a@x.com |   3 |       1 |
| b@x.com |   2 |       2 |
+---------+-----+---------+
2 rows in set (0.00 sec)

The check shows a@x.com 3 rows (keep 1) and b@x.com 2 rows (keep 2). Use a self join that deletes the row when a smaller id with the same email exists: DELETE m1 FROM members m1 JOIN members m2 ON m1.email = m2.email AND m1.id > m2.id. 3 rows are deleted and ids 1 and 2 remain.

After the fixrun as lab_a
DELETE m1 FROM members m1
JOIN members m2 ON m1.email = m2.email AND m1.id > m2.id;
SELECT id, email FROM members ORDER BY id;
✓ SUCCESS
Query OK, 3 rows affected (0.01 sec)

+----+---------+
| id | email   |
+----+---------+
|  1 | a@x.com |
|  2 | b@x.com |
+----+---------+
2 rows in set (0.00 sec)

03SET column = (SELECT AVG(...) FROM the same table)

Filling empty salaries with the department average uses a subquery on employees in SET. The error names the alias 'e' because the table was written as employees e.

Setup
Setuprun as lab_a
CREATE TABLE employees (id INT PRIMARY KEY, dept VARCHAR(10), salary INT);
INSERT INTO employees VALUES (1,'IT',4000),(2,'IT',5000),(3,'IT',NULL),(4,'HR',3000),(5,'HR',NULL);
OUTPUT
Query OK, 0 rows affected (0.01 sec)

Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
UPDATE employees e
SET salary = (SELECT AVG(salary) FROM employees WHERE dept = e.dept)
WHERE salary IS NULL;
✕ ERROR
ERROR 1093 (HY000): You can't specify target table 'e' for update in FROM clause
Checkrun as lab_a
SELECT dept, AVG(salary) AS avg_sal FROM employees GROUP BY dept;
OUTPUT
+------+-----------+
| dept | avg_sal   |
+------+-----------+
| IT   | 4500.0000 |
| HR   | 3000.0000 |
+------+-----------+
2 rows in set (0.00 sec)

Calculate the averages first (IT 4500, HR 3000) and join them: UPDATE employees e JOIN (SELECT dept, AVG(salary) AS avg_sal FROM employees GROUP BY dept) AS a ON a.dept = e.dept SET e.salary = a.avg_sal WHERE e.salary IS NULL. Ids 3 and 5 get 4500 and 3000.

After the fixrun as lab_a
UPDATE employees e
JOIN (SELECT dept, AVG(salary) AS avg_sal FROM employees GROUP BY dept) AS a ON a.dept = e.dept
SET e.salary = a.avg_sal
WHERE e.salary IS NULL;
SELECT id, dept, salary FROM employees ORDER BY id;
✓ SUCCESS
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

+----+------+--------+
| id | dept | salary |
+----+------+--------+
|  1 | IT   |   4000 |
|  2 | IT   |   5000 |
|  3 | IT   |   4500 |
|  4 | HR   |   3000 |
|  5 | HR   |   3000 |
+----+------+--------+
5 rows in set (0.00 sec)

04INSERT ... VALUES ((SELECT MAX(no) + 1 FROM the same table), ...)

To number a new post, the next post_no is computed with a subquery inside VALUES. That subquery reads board, the table being inserted into, so MySQL returns 1093 even though this is an INSERT, not an UPDATE.

Setup
Setuprun as lab_a
CREATE TABLE board (id INT AUTO_INCREMENT PRIMARY KEY, post_no INT NOT NULL, title VARCHAR(50));
INSERT INTO board (post_no, title) VALUES (1, 'hello'), (2, 'second');
OUTPUT
Query OK, 0 rows affected (0.01 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
INSERT INTO board (post_no, title)
VALUES ((SELECT MAX(post_no) + 1 FROM board), 'third');
✕ ERROR
ERROR 1093 (HY000): You can't specify target table 'board' for update in FROM clause
Checkrun as lab_a
SELECT MAX(post_no) + 1 AS next_no FROM board;
OUTPUT
+---------+
| next_no |
+---------+
|       3 |
+---------+
1 row in set (0.00 sec)

The value itself is fine (the query returns 3). Use INSERT ... SELECT instead of VALUES: INSERT INTO board (post_no, title) SELECT COALESCE(MAX(post_no), 0) + 1, 'third' FROM board. The row is added with post_no 3. COALESCE makes the first row get 1 when the table is empty.

After the fixrun as lab_a
INSERT INTO board (post_no, title)
SELECT COALESCE(MAX(post_no), 0) + 1, 'third' FROM board;
SELECT id, post_no, title FROM board ORDER BY id;
✓ SUCCESS
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

+----+---------+--------+
| id | post_no | title  |
+----+---------+--------+
|  1 |       1 | hello  |
|  2 |       2 | second |
|  3 |       3 | third  |
+----+---------+--------+
3 rows in set (0.00 sec)

FAQ

How do I fix You can't specify target table for update in FROM clause?

Rewrite the subquery as a JOIN: UPDATE t JOIN (SELECT ... FROM t ...) AS x ON ... SET .... For DELETE, join the table to itself.

How do I delete duplicate rows in MySQL and keep one?

DELETE m1 FROM t m1 JOIN t m2 ON m1.key = m2.key AND m1.id > m2.id; keeps the smallest id for each key. Check the counts with GROUP BY first.

Why does the error show an alias instead of the table name?

MySQL reports the name used in the statement. If you wrote employees e, the message says 'e'.

Can I update a table with the average of the same table?

Yes, compute the average in a derived table and JOIN it, as in the third case above.

Why do I get error 1093 on INSERT with SELECT MAX(id)+1?

A subquery inside VALUES that reads the target table is blocked. Write INSERT INTO t (no, ...) SELECT COALESCE(MAX(no), 0) + 1, ... FROM t instead; INSERT ... SELECT from the same table is allowed.

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