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:
- the NO_MERGE optimizer hint, for example
UPDATE /*+ NO_MERGE(dt) */ .... It applies only to this statement, which the UPDATE documentation points out as its advantage; SET optimizer_switch = 'derived_merge=off'for the session. This also affects every other query in the session, so set it back afterwards;- for a view,
ALGORITHM=TEMPTABLEin the view definition.
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:
- Materialization costs memory and disk. Forcing a derived table to materialize means MySQL builds an internal temporary table of all matching rows first. For millions of rows this temporary table can spill to disk. Select only the key columns you need inside the derived table.
- Locks follow the rows scanned, not the rows changed. The InnoDB locking documentation says that
UPDATE ... WHERE ...sets an exclusive next-key lock on every record the search encounters. A join that scans a big range locks that range until commit. Other sessions then wait and hit lock wait timeout exceeded. Make sure the join and filter use an index. - Multi-table statements cannot use LIMIT. Both the UPDATE and DELETE documentation say that
ORDER BYandLIMITare not allowed in the multiple-table form. ALIMITdirectly in anIN (...)subquery fails with error 1235 instead (subquery restrictions). The way out is to put theLIMITinside a derived table. That also prevents merging:
-- 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
- One huge statement is one huge transaction. In our experience a single UPDATE over tens of millions of rows produces a large undo log, a large row-based binary log event and a replica that falls behind for as long as the update ran. See our article on MySQL replication lag. Batches of a few thousand rows by primary key, with a short pause between them, keep each transaction small.
- Run it where it cannot surprise you. Measure one batch on a staging copy with production-size data, then run the batches during low traffic, and check affected rows after each one so you can stop at any point.
Key takeaways
- 1093 means a data-changing statement reads its own target table in a subquery.
- The "wrap it in another SELECT" trick works only if the derived table is materialized. Use
NO_MERGEinstead of relying on luck. - Prefer a plain WHERE condition, a multi-table UPDATE with a derived table, or a self-join DELETE.
- Check the rewrite with SELECT COUNT(*) before running it.
- On big tables: index the join, batch by primary key with LIMIT inside a derived table, and watch locks and replica lag.