Incident Response

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

MySQL will not let an UPDATE, DELETE or INSERT read the same table in a subquery. Here is why the usual "wrap it in another SELECT" trick sometimes stops working, the cleaner rewrites, and how to run the same change on a table with hundreds of millions of rows.

· 11 min read · AG Data Team

ERROR 1093 (HY000): You can't specify target table '...' for update in FROM clause means your UPDATE, DELETE or INSERT reads the same table it is changing, inside a subquery. MySQL refuses the statement before it touches any row. The fix is a rewrite, and on a large table the rewrite you pick decides whether the change takes a second or locks production for an hour.

mysql> UPDATE orders
    ->    SET status = 'archived'
    ->  WHERE id IN (SELECT id FROM orders
    ->               WHERE status = 'closed' AND closed_at < '2025-01-01');
ERROR 1093 (HY000): You can't specify target table 'orders' for update in FROM clause

Why MySQL refuses it

The subquery restrictions say it plainly: in general, you cannot modify a table and select from the same table in a subquery. That covers DELETE FROM t WHERE ... (SELECT ... FROM t), UPDATE t ... WHERE col = (SELECT ... FROM t) and the same pattern for INSERT and REPLACE. The error reference lists it as ER_UPDATE_TABLE_USED.

There is one documented exception. If the self-reference sits inside a derived table that MySQL materializes into a temporary table, the rows are already read when the update starts, so the statement is allowed. Note also that a plain INSERT INTO t ... SELECT ... FROM t is fine: the INSERT ... SELECT documentation says MySQL then buffers the selected rows in an internal temporary table. Only the subquery form is blocked.

Why the "extra SELECT" trick sometimes fails

The workaround everyone finds first is to wrap the subquery once more:

-- the classic workaround: wrap the subquery in a derived table
DELETE FROM sessions
 WHERE user_id IN (SELECT user_id FROM (
         SELECT user_id FROM sessions
          GROUP BY user_id HAVING COUNT(*) > 50) AS heavy);

This works because of materialization, and materialization is not guaranteed. The derived_merge optimizer flag is on by default, so MySQL tries to merge a derived table into the outer query. After a merge, the self-reference is back, and so is error 1093. The derived table optimization page lists the constructs that prevent merging: aggregate or window functions, DISTINCT, GROUP BY, HAVING, LIMIT, UNION, subqueries in the select list, assignments to user variables, and references only to literal values. The example above works because it has GROUP BY. A plain SELECT id FROM (SELECT id FROM t WHERE ...) AS x can be merged and fail.

The explicit ways to force materialization are:

Better rewrites

Most 1093 statements do not need a subquery at all. Before you reach for tricks, check whether the subquery reads the same rows the outer statement already filters. If it does, put the condition in the WHERE clause directly. When you really need values from other rows of the same table, a multi-table statement with a derived table is clearer:

-- 1. simplest: no subquery at all
UPDATE orders
   SET status = 'archived'
 WHERE status = 'closed' AND closed_at < '2025-01-01';

-- 2. a real self-reference: mark each customer's latest order,
--    multi-table UPDATE with a materialized derived table
--    (GROUP BY already prevents merging; the hint makes it explicit)
UPDATE /*+ NO_MERGE(latest) */ orders o
  JOIN (SELECT customer_id, MAX(created_at) AS last_at
          FROM orders GROUP BY customer_id) AS latest
    ON latest.customer_id = o.customer_id
   AND latest.last_at = o.created_at
   SET o.is_latest = 1;

For deleting duplicates, a self-join in a multi-table DELETE needs no subquery at all. The DELETE documentation requires the alias to be declared in the table references, as here:

-- delete duplicate emails, keep the row with the lowest id (self-join DELETE)
DELETE u1
  FROM users AS u1
  JOIN users AS u2
    ON u1.email = u2.email
   AND u1.id > u2.id;

Test the rewrite with SELECT first. Turn the UPDATE or DELETE into a SELECT COUNT(*) with the same joins and conditions, and compare the number with what you expect. A wrong join in a multi-table DELETE removes rows from the wrong side, and there is no undo once you commit.

What changes on large tables

On a test table with a thousand rows, any of these rewrites finishes instantly. On a production table with hundreds of millions of rows, the same statement behaves very differently:

-- one batch: at most 5000 rows, by primary key
UPDATE orders o
  JOIN (SELECT id FROM orders
         WHERE status = 'closed' AND closed_at < '2025-01-01'
         ORDER BY id
         LIMIT 5000) AS batch USING (id)
   SET o.status = 'archived';
-- repeat until 0 rows are affected; pause between batches and watch replica lag

Key takeaways

MySQL Errors SQL Performance
Work with us

Big UPDATE or DELETE to run in production?

We plan and run large MySQL data changes in safe batches, with lock, undo and replication lag under control, so production keeps working.

Get in touch More from the blog