# UPDATE & DELETE

> Change and remove rows safely: how WHERE decides which rows, what the row count tells you, and why a missing WHERE is dangerous.

Subject: SQL | Lesson 30 of 44 | 5 min read
Interactive lesson: https://codewithanimation.in/sql/update-and-delete
Previous: [INSERT](https://codewithanimation.in/sql/insert-rows.md) | Next: [RETURNING & Upsert](https://codewithanimation.in/sql/returning-and-upsert.md)

## Change rows and remove rows

`UPDATE table SET column = value WHERE condition` changes the rows that match. `DELETE FROM table WHERE condition` removes the rows that match. Both use the same `WHERE` you already know.

The new value in `SET` can use the old value of the row, as in `SET price = price * 1.10`. You can set several columns at once, separated by commas.

## Data changes

People change their email, orders are cancelled, accounts are closed. UPDATE and DELETE are how a database keeps up. They are also the statements that can do the most damage by accident.

## One row, then every row

```sql
UPDATE members
SET email = 'ben@new.test'
WHERE id = 2;
```

The tag `UPDATE 1` says one row changed. Because `id` is the primary key, `WHERE id = 2` addresses exactly one row. Only a primary key lets you target one row reliably.

Leave out the `WHERE` and every row is changed: `UPDATE members SET name = upper(name)` shows `UPDATE 3`. `DELETE FROM members` with no `WHERE` deletes every row. The manual's own comment is "Caveat programmer".

A `WHERE` that matches no rows is not an error. You simply get `UPDATE 0` or `DELETE 0`.

## Common mistake: Silent accidents

A forgotten `WHERE` runs without any warning, and outside a transaction there is no undo. The next lessons show how to protect yourself with transactions.

A typo in the `WHERE`, such as the wrong id, also runs without an error. Always compare the count in the tag with the number you expected.

A statement is all or nothing. If one of the rows would break a rule, for example a UNIQUE email, none of the rows are changed, even those that were fine.

## Tip: SELECT first, then change

Write the `WHERE` first and run it as a `SELECT`. When the rows are the ones you want, change `SELECT *` into `UPDATE ... SET` or `DELETE`, keeping the same `WHERE`. Then check that the count in the tag matches.

## Try it: Change a few rows, then a lot

Run the UPDATE with WHERE, then without. Try the UPDATE that matches nothing and the one that breaks UNIQUE. Press Reset table whenever you want a fresh start.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/update-and-delete

## Check yourself

### Question 1

`UPDATE members SET name = 'X'` is run with no WHERE on a table of 3 rows. What happens?

- A. All 3 rows are changed, with no error
- B. PostgreSQL refuses to run it
- C. Only the first row is changed

Correct answer: A

The manual says that if WHERE is omitted, all rows in the table are updated.

- Why not B: There is no safety check. A missing WHERE simply means all rows.
- Why not C: UPDATE has no notion of a first row. Without WHERE it applies to every row.

### Question 2

`DELETE FROM members WHERE id = 99` matches no row. What does it do?

- A. Reports DELETE 0 and raises no error
- B. Raises an error because the row is missing
- C. Deletes the closest id

Correct answer: A

A statement that matches no rows is valid. The tag shows the count, which is 0.

- Why not B: Changing zero rows is not an error. Check the count in the tag instead.
- Why not C: WHERE only matches rows that satisfy the condition exactly.

### Question 3

An UPDATE would change rows 2 and 3, but row 2 would break a UNIQUE rule. What is the result?

- A. Neither row is changed and an error is raised
- B. Row 3 is changed and row 2 is skipped
- C. Both rows are changed anyway

Correct answer: A

If any row fails, PostgreSQL cancels the whole statement, so the table is exactly as it was.

- Why not B: A statement is all or nothing. It never changes only some of its rows.
- Why not C: The UNIQUE rule is always enforced.

## Remember this

- UPDATE ... SET ... WHERE changes matching rows. DELETE FROM ... WHERE removes them.
- With no WHERE, every row is changed or removed, and there is no warning.
- A WHERE that matches no rows is not an error. The tag shows 0.
- A statement is all or nothing. Run the WHERE as a SELECT first, and check the count.
