UPDATE
Objectives
By the end of this lesson, you should be able to:
- Change existing rows with
UPDATE - Explain why
WHEREmatters forUPDATE - Update more than one column at once
💡 Why this matters: Data changes, an employee gets a raise, moves departments, changes their email.
UPDATEis how existing rows are changed without deleting and re-inserting them.
⚠️ A note on verification: every statement and result in this lesson was run against a real, live PostgreSQL 18 database, using the
employeestable from Module 3, filtered withWHERE(covered fully in Module 6).
The UPDATE Statement
UPDATE employees SET salary = 85000 WHERE first_name = 'Erin';
first_name | salary
------------+---------
Erin | 85000.00
UPDATE employees SET salary = 85000 changes the salary column, WHERE first_name = 'Erin' limits it to matching rows only. Without a value like 85000, an expression works too, SET salary = salary * 1.05 (a 5% raise, calculated from each row’s current value) is just as valid as a literal.
Why WHERE Matters
Leaving off WHERE updates every single row in the table:
UPDATE employees SET is_active = true;
This sets is_active to true for every row, not just one. That’s sometimes exactly what’s intended, but it’s also one of the most common, and most damaging, mistakes in real SQL work, running an UPDATE meant for one row without a WHERE clause narrowing it down. Always double-check a WHERE clause is present, and correct, before running an UPDATE against real data.
Updating Multiple Columns
Separate each column = value pair with a comma:
UPDATE employees SET department = 'Product', salary = 60000 WHERE first_name = 'Priya';
first_name | department | salary
------------+------------+---------
Priya | Product | 60000.00
Both columns change together, in the same statement, against the same matched row.
Try It
- Update one employee’s
salaryto a new value, and confirm the change with aSELECT. - Update one employee’s
departmentandsalarytogether, in a singleUPDATEstatement. - Explain, in your own words, why running an
UPDATEwith noWHEREclause is risky, and give an example of when it would actually be the right thing to do.
Recap
UPDATE table SET column = value WHERE condition;changes matching rows.- Leaving off
WHEREupdates every row in the table, always double-check it before running against real data. - Several columns can be updated together by separating
column = valuepairs with commas.
Next lesson: DELETE, removing rows entirely.