UPDATE & DELETE
Change and remove rows — and why WHERE is not optional
Changing and Removing Data
UPDATE modifies existing rows; DELETE removes them. Both are dangerous when used without a WHERE clause — a single missing keystroke can wipe a table. Here's how to use them safely:
UPDATE t SET col = value WHERE condition. The SET clause lists columns to change and their new values. The WHERE clause picks which rows are affected. Leave out the WHERE, and *every row* in the table gets the new value.UPDATE products SET price = price * 1.10 WHERE category = 'Electronics' raises electronics prices by 10%. UPDATE orders SET status = 'archived', archived_at = DATE('now') WHERE order_date < '2023-01-01' updates multiple columns at once.DELETE FROM t WHERE condition. Same structure, same warning: DELETE FROM orders; with no WHERE empties the entire table silently. Run SELECT COUNT(*) FROM t WHERE condition first to see exactly how many rows will be removed.SELECT * FROM orders WHERE status = 'pending' AND order_date < '2023-01-01'. If that returns 50,000 rows, you know what your DELETE is about to do. Professionals never skip this step.BEGIN ... COMMIT groups multiple changes into an atomic unit: if anything fails, ROLLBACK reverts everything. Essential when a single logical change spans multiple statements ("move row from active to archive" = DELETE + INSERT, and you want both or neither).TRUNCATE TABLE t empties a table faster than DELETE FROM t because it doesn't log each row. But it's a DDL operation — it can't be rolled back in MySQL, bypasses DELETE triggers, and resets auto-increment counters. SQLite doesn't have TRUNCATE; use DELETE FROM t instead.How UPDATE & DELETE Work
Change and remove rows
- UPDATE t SET col = value WHERE condition — change matching rows
- DELETE FROM t WHERE condition — remove matching rows
- Without WHERE, the statement affects EVERY row
- Both are immediately effective — there is no undo button
Catastrophic Traps
The most dangerous SQL
- UPDATE without WHERE → modifies every row silently
- DELETE without WHERE → empties the table silently
- DELETE with CASCADE FKs → may delete from other tables too
- Forgetting COMMIT in a transaction → changes invisible / rolled back
The Dry-Run Habit
What professionals always do first
- Run your WHERE clause as a SELECT first — see exactly what will change
- Check COUNT(*) to confirm the row count matches expectations
- Wrap risky changes in BEGIN ... COMMIT / ROLLBACK
- Consider soft-delete: UPDATE status = 'deleted' instead of DELETE
UPDATE vs DELETE vs TRUNCATE
When to use which
UPDATE ... SET ... WHEREChange values in existing rows — WHERE picks whichDELETE FROM ... WHERERemove rows — WHERE picks whichSET col = col * 1.10New value can reference the current valueBEGIN ... COMMITAtomic unit — all changes apply together, or ROLLBACK undoesWorked Example
Set up the staff table, give every Engineer a 10% raise, then remove anyone earning under 80,000.
| id | name | role | salary |
|---|---|---|---|
| 1 | Aarav | Engineer | 99000 |
| 3 | James | Engineer | 104500 |
| 4 | Priya | Manager | 120000 |
UPDATE without WHERE
Sets status to 'cancelled' on *every* row in the table. No error, no warning. This is the most common SQL disaster.
Always write the WHERE first: UPDATE orders SET status = 'cancelled' WHERE id = 5001;
DELETE without WHERE
Empties the entire customers table. Even worse: if you have FK constraints with CASCADE, this can cascade-delete orders, order_items, and reviews too.
DELETE FROM customers WHERE id = 'C1001'; — or better, run SELECT with the same WHERE first to see what you're about to delete.
Forgetting transactions for multi-step changes
Moving a row from active to archive involves INSERT + DELETE. If the server crashes between the two, you either lose the row or have it in both places.
Wrap them in BEGIN ... COMMIT so both succeed together or neither does.
Key Concepts
Pro Tip
UPDATE and DELETE are how data evolves over time. They're also where most catastrophic data losses begin — a forgotten WHERE clause can destroy months of work in milliseconds.
When to Use
Correcting data quality bugs, applying price changes, archiving old records, anonymizing user data, soft-deleting accounts (UPDATE status = 'deleted'), cleaning up test rows.