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:

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:

Symptom 3: "the wrong rows changed"

Common causes:

Safe habits that prevent all three

  1. SELECT first. Write the WHERE clause as a SELECT, check the rows, then change SELECT to UPDATE or DELETE.
  2. Use a transaction while learning:
BEGIN;
DELETE FROM students WHERE id = 42;
SELECT COUNT(*) FROM students;   -- check
COMMIT;   -- or ROLLBACK; if something looks wrong
  1. Target rows by primary key when you mean one specific row.
  2. Practise on a copy. With SQLite, copy the database file before experimenting.
  3. Keep backups of anything you care about.

A quick symptom table

What you sawFirst thing to check
0 rows affectedRun the WHERE clause as a SELECT
All rows affectedWas there a WHERE clause at all?
Too many rowsBrackets around OR, non-unique columns
A NULL row was skippedIS 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.