MySQL UPDATE and DELETE: Joins, LIMIT and Safe Updates

MySQL 55 min min read Updated: Nov 27, 2026 Intermediate
MySQL UPDATE and DELETE: Joins, LIMIT and Safe Updates
Intermediate Topic 2 of 3

UPDATE Basics

sql UPDATE products SET price = price * 1.10, stock = stock + 5 WHERE category = 'Furniture'; SELECT id, name, price, stock FROM products WHERE category = 'Furniture' ORDER BY id;

Result:

idnamepricestock
4Desk273.9015
5Chair141.9030
8Lamp49.505

Always check the same condition with SELECT first. A missing WHERE changes every row.

UPDATE with a JOIN

MySQL lets you update one table using columns from another with UPDATE ... JOIN ... SET. Add a loyalty column and give 5 points per non-cancelled order:

sql ALTER TABLE customers ADD COLUMN points INT NOT NULL DEFAULT 0; UPDATE customers c JOIN (SELECT customer_id, COUNT(*) AS n FROM orders WHERE status <> 'cancelled' GROUP BY customer_id) t ON t.customer_id = c.id SET c.points = t.n * 5; SELECT id, name, points FROM customers ORDER BY id;

Result:

idnamepoints
1Asha Rao15
2Ben Carter10
3Chen Wei10
4Dina Cohen0
5Eli Brown5
6Fatima Khan5

The joined table is a derived table that counts orders. Customers without orders are not in the join, so they keep their default of 0.

UPDATE with ORDER BY and LIMIT

MySQL allows ORDER BY and LIMIT in a single-table UPDATE or DELETE, which is handy for batches:

sql UPDATE products SET stock = stock + 100 ORDER BY price ASC LIMIT 2; SELECT id, name, price, stock FROM products ORDER BY price LIMIT 3;

Result:

idnamepricestock
6Notebook3.50600
7Pen Pack5.00400
8Lamp45.000

Only the two cheapest products were touched. Deleting or updating in batches of 1,000 rows keeps transactions short on big tables.

DELETE Basics

sql INSERT INTO products VALUES (9, 'Old Gadget', 'Electronics', 10.00, 0); DELETE FROM products WHERE stock = 0 AND id = 9; SELECT COUNT(*) AS products_left FROM products;

Result:

products_left
8

DELETE with a JOIN

Delete rows of one table based on another. For example remove all items of cancelled orders:

sql DELETE oi FROM order_items oi JOIN orders o ON o.id = oi.order_id WHERE o.status = 'cancelled'; SELECT order_id, product_id FROM order_items WHERE order_id = 5; SELECT COUNT(*) AS items_left FROM order_items;

Result:

Result

(0 rows)

Result:

items_left
15

DELETE oi FROM ... names which table loses rows. You can even delete from two tables in one statement:

Tables linked by a foreign key can make a two-table delete fail, because InnoDB checks the key row by row (error 1451). It works for tables without that key, or when you delete child rows first:

sql CREATE TABLE carts (id INT PRIMARY KEY, status VARCHAR(10)); CREATE TABLE cart_items (id INT PRIMARY KEY, cart_id INT); INSERT INTO carts VALUES (1, 'open'), (2, 'abandoned'); INSERT INTO cart_items VALUES (1, 1), (2, 2), (3, 2); DELETE c, ci FROM carts c JOIN cart_items ci ON ci.cart_id = c.id WHERE c.status = 'abandoned'; SELECT (SELECT COUNT(*) FROM carts) AS carts_left, (SELECT COUNT(*) FROM cart_items) AS items_left;

Result:

carts_leftitems_left
11

Deleting from a Table Using Itself

MySQL does not allow a subquery on the same table you modify (error 1093). Wrap it in a derived table:

sql CREATE TABLE signups (id INT PRIMARY KEY, email VARCHAR(30)); INSERT INTO signups VALUES (1,'a'), (2,'a'), (3,'b'); DELETE FROM signups WHERE id NOT IN (SELECT MIN(id) FROM signups GROUP BY email);
Error

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

sql CREATE TABLE signups (id INT PRIMARY KEY, email VARCHAR(30)); INSERT INTO signups VALUES (1,'a'), (2,'a'), (3,'b'); DELETE FROM signups WHERE id NOT IN (SELECT keep_id FROM (SELECT MIN(id) AS keep_id FROM signups GROUP BY email) AS k); SELECT id, email FROM signups ORDER BY id;

Result:

idemail
1a
3b

TRUNCATE vs DELETE

DELETETRUNCATE
ConditionCan use WHEREWhole table only
SpeedRow by row, slowerVery fast
AUTO_INCREMENTKeeps countingRestarts at 1
RollbackYes, inside a transactionNo, it commits (DDL)
Foreign keysAllowed (rows checked)Refused if another table references it
sql CREATE TABLE logs (id INT AUTO_INCREMENT PRIMARY KEY, msg VARCHAR(20)); INSERT INTO logs (msg) VALUES ('a'), ('b'), ('c'); DELETE FROM logs; INSERT INTO logs (msg) VALUES ('d'); SELECT id, msg FROM logs;

Result:

idmsg
4d
sql CREATE TABLE logs (id INT AUTO_INCREMENT PRIMARY KEY, msg VARCHAR(20)); INSERT INTO logs (msg) VALUES ('a'), ('b'), ('c'); TRUNCATE TABLE logs; INSERT INTO logs (msg) VALUES ('d'); SELECT id, msg FROM logs;

Result:

idmsg
1d
sql TRUNCATE TABLE customers;
Error

ERROR 1701: Cannot truncate a table referenced in a foreign key constraint ('shop'.'orders', CONSTRAINT 'orders_ibfk_1')

Safe Updates Mode

The mysql client has an option --safe-updates (also --i-am-a-dummy) that refuses an UPDATE or DELETE without a key condition or LIMIT. MySQL Workbench enables it by default. You can turn it on for a session:

sql SET SESSION sql_safe_updates = 1; UPDATE products SET price = price + 1;
Error

ERROR 1175: You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column.

sql SET SESSION sql_safe_updates = 1; UPDATE products SET price = price + 1 WHERE id = 1; SELECT id, price FROM products WHERE id = 1;

Result:

idprice
1900.00

Rows Matched vs Rows Changed

MySQL reports both: rows matched by WHERE and rows changed. If the new value equals the old one, the row matches but does not change:

sql UPDATE products SET price = 899.00 WHERE id = 1; SELECT ROW_COUNT() AS rows_changed;

Result:

rows_changed
0

Zero rows changed, although one matched. Application code should not treat "0 rows affected" as "row not found".

Common Beginner Mistakes

MistakeWhat happensFix
No WHERE on UPDATE or DELETEEvery row changesSELECT first, use sql_safe_updates and transactions
Subquery on the same table in DELETEError 1093Wrap in a derived table or use DELETE ... JOIN
TRUNCATE inside a transactionCannot be undoneUse DELETE when you need rollback
Reading 0 rows affected as an errorFalse alarmsRemember unchanged rows count as 0
Deleting millions of rows at onceLong locks, huge undo logDelete in batches with LIMIT

Practice

  1. Give each customer a last_order date column filled with their latest order date (UPDATE with JOIN).
  2. Delete order items of orders older than 2025-02-01 using a JOIN delete.
  3. Remove duplicate emails from a table, keeping the lowest id.
  4. Turn on sql_safe_updates and try an update without a key.

Conclusion

MySQL extends UPDATE and DELETE with joins, ORDER BY and LIMIT, refuses same-table subqueries, and offers sql_safe_updates. Check with SELECT first, use transactions, and do large changes in batches.

Quick Summary: UPDATE JOIN, DELETE JOIN, LIMIT, TRUNCATE, safe updates, error 1093, rows affected.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators