UPDATE — Changing Rows in SQL
Edit values in place — always carry a WHERE
UPDATE changes values in existing rows: SET assigns new expressions to columns, WHERE chooses which rows. Forget the WHERE and every row in the table gets the same change — the most classic accident in SQL. Professional habit: run the identical condition as a SELECT first, preview the affected rows, then commit the UPDATE.
Name the target table
UPDATE employees SET … — one table per UPDATE.
List the assignments
SET salary = 55000 — or expressions referencing old values: SET salary = salary * 1.10.
Restrict with WHERE
WHERE id = 2 targets exactly one row. No WHERE means EVERY row changes — the most classic accident in SQL.
Preview before you commit
Professional habit: run SELECT * FROM employees WHERE <same condition> first, eyeball the affected rows, then run the UPDATE.
Wrap risky edits in transactions
BEGIN; UPDATE …; check; COMMIT — or ROLLBACK if the affected-rows count looks wrong.
1UPDATE employees2SET salary = 550003WHERE id = 2;
| id | name | department | salary | age |
|---|---|---|---|---|
| 1 | Alice | Engineering | 85000 | 29 |
| 2 | Bob | Sales | 55000 | 35 |
| 3 | Cynthia | Engineering | 91000 | 41 |
| 4 | David | Marketing | 47000 | 38 |
| 5 | Elena | Sales | 58000 | 26 |
| 6 | Farid | Engineering | 73000 | 33 |
Exactly one row matches id = 2, so Bob’s salary flips 52000 → 55000. The visualization highlights the touched cell and shows the before → after.
One row by primary key
The safest update — a unique key in the WHERE.
1UPDATE employees2SET salary = 550003WHERE id = 2;
| id | name | department | salary | age |
|---|---|---|---|---|
| 1 | Alice | Engineering | 85000 | 29 |
| 2 | Bob | Sales | 55000 | 35 |
| 3 | Cynthia | Engineering | 91000 | 41 |
| 4 | David | Marketing | 47000 | 38 |
| 5 | Elena | Sales | 58000 | 26 |
| 6 | Farid | Engineering | 73000 | 33 |
Raise everyone 5%
SET can compute from the old value.
1UPDATE employees2SET salary = salary * 1.05;
| id | name | department | salary | age |
|---|---|---|---|---|
| 1 | Alice | Engineering | 89250 | 29 |
| 2 | Bob | Sales | 54600 | 35 |
| 3 | Cynthia | Engineering | 95550 | 41 |
| 4 | David | Marketing | 49350 | 38 |
| 5 | Elena | Sales | 60900 | 26 |
| 6 | Farid | Engineering | 76650 | 33 |
Multiple columns
Comma-separated assignments in one SET.
1UPDATE employees2SET department = 'Marketing',3 salary = 610004WHERE id = 5;
| id | name | department | salary | age |
|---|---|---|---|---|
| 1 | Alice | Engineering | 85000 | 29 |
| 2 | Bob | Sales | 52000 | 35 |
| 3 | Cynthia | Engineering | 91000 | 41 |
| 4 | David | Marketing | 47000 | 38 |
| 5 | Elena | Marketing | 61000 | 26 |
| 6 | Farid | Engineering | 73000 | 33 |
Guarded with a transaction
The production safety net.
1BEGIN;2UPDATE employees SET salary = 99000 WHERE id = 3;3SELECT * FROM employees WHERE id = 3;4-- looks wrong? ROLLBACK;5COMMIT;
| id | name | department | salary | age |
|---|---|---|---|---|
| 3 | Cynthia | Engineering | 99000 | 41 |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.