UPDATE is the U in CRUD. It's also the statement most likely to ruin your day if you forget one clause.
1UPDATE tasks
2SET status = 'DONE',
3 updated_at = now()
4WHERE id = 42;SET lists the columns to change.WHERE chooses which rows. This is not optional in practice.1UPDATE tasks SET status = 'DONE'; -- 😱 updates EVERY rowForget the WHERE and you change the whole table. Habits that save you:
WHERE clause first, then the SET.SELECT with the same WHERE to preview the affected rows.ROLLBACK.1UPDATE tasks
2SET status = 'DONE', updated_at = now()
3WHERE id = 42
4RETURNING *;RETURNING * hands back the updated row — handy for sending the fresh state straight to the client.
1-- Only advance tasks that are actually in progress
2UPDATE tasks
3SET status = 'DONE'
4WHERE id = 42 AND status = 'IN_PROGRESS';
5
6-- Compute from the current value
7UPDATE users SET login_count = login_count + 1 WHERE id = 7;The second form is atomic — the database reads and writes in one step, avoiding lost updates under concurrency.
UPDATE returns the number of rows affected. If it's 0, the WHERE matched nothing — often the right moment to return a 404 from your API.