Troubleshooting an UPDATE or DELETE That Changed the Wrong Rows
UPDATE changes existing rows and DELETE removes them. Both depend completely on their WHERE clause, the part that says which rows to touch. When they go wrong, it is almost always the WHERE clause. This page helps you work out what happened and how to stay safe next time.
Symptom 1: "0 rows affected"
Your statement ran but changed nothing.
Common causes:
- The WHERE clause matches no rows. A typo in a value, such as
'Londn', or a different capitalisation in databases that compare text exactly. - Extra spaces in stored values.
'Ana 'with a trailing space is not equal to'Ana'. - Wrong table or database. You are connected to a test copy, or a different database than you think.
How to check: run a SELECT with the same WHERE clause first:
SELECT * FROM students WHERE city = 'London';
If it returns nothing, the UPDATE will change nothing.
Symptom 2: "every row changed"
This is the scary one.
Common cause: the WHERE clause was missing, or was accidentally separated from the statement. UPDATE students SET grade = 'A'; with no WHERE updates every student.
What to do now:
- If you were inside a transaction (you typed
BEGINfirst), typeROLLBACK;to undo it. - If not, the change is already saved. Restore from your latest backup or copy. This is why practising on a copy matters.
Symptom 3: "the wrong rows changed"
Common causes:
- Mixing AND and OR without brackets.
WHERE class = 'A' OR class = 'B' AND year = 2is read asclass = 'A' OR (class = 'B' AND year = 2). Use brackets to say what you mean:WHERE (class = 'A' OR class = 'B') AND year = 2. - Matching on a non-unique column.
WHERE name = 'Sam'updates every Sam. Use the primary key, such asWHERE id = 42, when you mean one row. - NULL surprises.
WHERE email = NULLnever matches anything; useWHERE email IS NULL.
Safe habits that prevent all three
- SELECT first. Write the WHERE clause as a SELECT, check the rows, then change SELECT to UPDATE or DELETE.
- Use a transaction while learning:
BEGIN;
DELETE FROM students WHERE id = 42;
SELECT COUNT(*) FROM students; -- check
COMMIT; -- or ROLLBACK; if something looks wrong
- Target rows by primary key when you mean one specific row.
- Practise on a copy. With SQLite, copy the database file before experimenting.
- Keep backups of anything you care about.
A quick symptom table
| What you saw | First thing to check |
|---|---|
| 0 rows affected | Run the WHERE clause as a SELECT |
| All rows affected | Was there a WHERE clause at all? |
| Too many rows | Brackets around OR, non-unique columns |
| A NULL row was skipped | IS NULL instead of = NULL |
Learn more
Our long read on create, read, update and delete explains each statement in more detail, with small examples you can run.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.