Learn SQL
-- modifying.update

UPDATE — Changing Rows in SQL

Edit values in place — always carry a WHERE

--concept

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.

--how it works
1

Name the target table

UPDATE employees SET … — one table per UPDATE.

2

List the assignments

SET salary = 55000 — or expressions referencing old values: SET salary = salary * 1.10.

3

Restrict with WHERE

WHERE id = 2 targets exactly one row. No WHERE means EVERY row changes — the most classic accident in SQL.

4

Preview before you commit

Professional habit: run SELECT * FROM employees WHERE <same condition> first, eyeball the affected rows, then run the UPDATE.

5

Wrap risky edits in transactions

BEGIN; UPDATE …; check; COMMIT — or ROLLBACK if the affected-rows count looks wrong.

--visualization
idnamedepartmentsalary1AliceEngineering850002BobSales520003CynthiaEngineering910004DavidMarketing470005ElenaSales580006FaridEngineering73000-- preview: SELECT * FROM employees WHERE id = 2;
matched row updated
Stage 1 / 4
Preview first! SELECT * WHERE id = 2 → exactly Bob's row. Now we can safely UPDATE.
--example
query.sql
1UPDATE employees
2SET salary = 55000
3WHERE id = 2;
Result
idnamedepartmentsalaryage
1AliceEngineering8500029
2BobSales5500035
3CynthiaEngineering9100041
4DavidMarketing4700038
5ElenaSales5800026
6FaridEngineering7300033

Exactly one row matches id = 2, so Bob’s salary flips 52000 → 55000. The visualization highlights the touched cell and shows the before → after.

--variants

One row by primary key

The safest update — a unique key in the WHERE.

query.sql
1UPDATE employees
2SET salary = 55000
3WHERE id = 2;
Result
idnamedepartmentsalaryage
1AliceEngineering8500029
2BobSales5500035
3CynthiaEngineering9100041
4DavidMarketing4700038
5ElenaSales5800026
6FaridEngineering7300033
1Exactly one row matches id = 2.
2Bob's salary overwrites 52000 → 55000.
3The engine reports "1 row affected" — always read that count.

Raise everyone 5%

SET can compute from the old value.

query.sql
1UPDATE employees
2SET salary = salary * 1.05;
Result
idnamedepartmentsalaryage
1AliceEngineering8925029
2BobSales5460035
3CynthiaEngineering9555041
4DavidMarketing4935038
5ElenaSales6090026
6FaridEngineering7665033
1No WHERE — intentional here: every row gets the raise.
2Each salary is read, multiplied, written back.
3The affected-rows count should equal the whole table — if not, something else changed.

Multiple columns

Comma-separated assignments in one SET.

query.sql
1UPDATE employees
2SET department = 'Marketing',
3 salary = 61000
4WHERE id = 5;
Result
idnamedepartmentsalaryage
1AliceEngineering8500029
2BobSales5200035
3CynthiaEngineering9100041
4DavidMarketing4700038
5ElenaMarketing6100026
6FaridEngineering7300033
1Two assignments apply together, atomically per row.
2Elena moves from Sales to Marketing at 61000 in one statement.
3Assignments see the OLD values — no cascading within one SET.

Guarded with a transaction

The production safety net.

query.sql
1BEGIN;
2UPDATE employees SET salary = 99000 WHERE id = 3;
3SELECT * FROM employees WHERE id = 3;
4-- looks wrong? ROLLBACK;
5COMMIT;
Result
idnamedepartmentsalaryage
3CynthiaEngineering9900041
1BEGIN opens a sandbox: only you can see the change.
2The SELECT inside previews the new row before anyone else sees it.
3COMMIT publishes; ROLLBACK pretends it never happened.
--key facts
Golden ruleno WHERE = table-wide edit; some teams require it via lint rules
ExpressionsSET salary = salary * 1.05 gives everyone a 5% raise in one line
Safety netwrap risky updates in a transaction and review before COMMIT
--use cases
Editing profile fields, prices, statuses — any in-place change
Bulk corrections: SET status = 'shipped' WHERE order_date < '2024-01-01'
Give-everyone raises / reprice a catalogue via expressions
Migration scripts that backfill new columns with computed values
--under the hood
Row-level locking: each updated row locks until the transaction ends — long transactions stall other writers.
The affected-rows count is your smoke detector: expected 1, got 600? ROLLBACK and re-read the WHERE.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.