advanced
UPDATE
Modify existing rows in a table with UPDATE — change one column or many, for one row or millions.
5 min read
Explanation
UPDATE changes values in existing rows. You name the table, set one or more
columns to new values (which can be expressions), and — critically — filter with
WHERE to limit which rows change.
Without a WHERE, every row is updated, which is rarely what you want. A
safe habit: write the SELECT version of your filter first, confirm the rows,
then convert it to an UPDATE.
Preview before you update
Run SELECT * FROM table WHERE ... with your intended filter. If those are the
rows you mean to change, turn it into an UPDATE inside a transaction.
Syntax
UPDATE products
SET price = price * 1.10,
stock = stock + 10
WHERE category = 'Electronics';
-- Update from another table:
UPDATE employees e
SET salary = e.salary + 5000
FROM departments d
WHERE e.department_id = d.id AND d.name = 'Engineering';Interactive Example
Preview what a 10% price increase would look like for Electronics products before you'd ever run the update.
Loading database engine...
Loading database engine...
Common Mistakes
- Omitting WHERE. The update hits every row — a classic, painful mistake.
- Updating the wrong column. Double-check column names; a typo can zero out or null out data.
- Not wrapping in a transaction. For anything important,
BEGINfirst so you canROLLBACKif the result is wrong.
Best Practices
- Always specify a
WHERE; if you truly want all rows, say so deliberately. - Preview with
SELECTusing the same filter before committing. - Use expressions (
price * 1.10) to compute new values from old ones. - Run updates inside a transaction and verify before
COMMIT.
Practice Question
Write an UPDATE that reduces the stock of every 'Stationery' product by 25
(use stock - 25), and describe the WHERE clause you'd use to limit it.
Summary
UPDATE modifies existing rows via SET column = value and a WHERE filter.
Never skip the WHERE, preview with SELECT, and prefer a transaction for
reversible, safe changes.