CodingNic

CRUD Operations

UPDATE

CRUD Operations 10 min read

UPDATE

Objectives

By the end of this lesson, you should be able to:

  • Change existing rows with UPDATE
  • Explain why WHERE matters for UPDATE
  • Update more than one column at once

💡 Why this matters: Data changes, an employee gets a raise, moves departments, changes their email. UPDATE is 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 employees table from Module 3, filtered with WHERE (covered fully in Module 6).

The UPDATE Statement

sql
UPDATE employees SET salary = 85000 WHERE first_name = 'Erin';
text
 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:

sql
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:

sql
UPDATE employees SET department = 'Product', salary = 60000 WHERE first_name = 'Priya';
text
 first_name | department | salary
------------+------------+---------
 Priya      | Product    | 60000.00

Both columns change together, in the same statement, against the same matched row.

Try It

  1. Update one employee’s salary to a new value, and confirm the change with a SELECT.
  2. Update one employee’s department and salary together, in a single UPDATE statement.
  3. Explain, in your own words, why running an UPDATE with no WHERE clause 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 WHERE updates every row in the table, always double-check it before running against real data.
  • Several columns can be updated together by separating column = value pairs with commas.

Next lesson: DELETE, removing rows entirely.