Skip to content
C

UPDATE


UPDATE

Definition

UPDATE modifies the values of existing rows in a table, without changing which rows exist. Like DELETE, its WHERE clause is what makes it targeted instead of total.

How It Works

sql
UPDATE employees SET salary = FLOOR(salary * 1.1) WHERE department = 'Engineering';

This gives every Engineering employee a 10% raise, computed from their OWN current salary — SET salary = FLOOR(salary * 1.1) reads each row's existing value before writing the new one, row by row. Multiple columns can be updated in one statement (SET salary = ..., department = ...), and every row matching the WHERE clause is updated in a single, atomic statement.

Edge Cases and Pitfalls

  • Running UPDATE with NO WHERE clause updates EVERY row in the table — exactly the same catastrophic-by-omission risk as DELETE with no WHERE (11.5). Always double-check the WHERE clause, or preview affected rows with an equivalent SELECT first.
  • SET column = column + 1-style updates (referencing the column's own current value) are extremely common and work correctly because the RIGHT-hand side is evaluated using each row's existing value before that row's SET takes effect.
  • An UPDATE can fail partway through a large batch if one row's new value violates a constraint (e.g. a CHECK or UNIQUE constraint) — like INSERT, this rolls back the whole statement's effect by default, not just the offending row.

Key Takeaways

  • UPDATE table SET col = value WHERE condition modifies matching rows' values in place.
  • Omitting WHERE updates every row — the single most damaging accidental SQL mistake, shared with DELETE.
  • SET col = col + expr patterns correctly reference each row's own pre-update value.

Mock Test

  • UPDATE - Quick Test

    8 questions on UPDATE.

    8 questions · 8 min · Medium
    Start Mock Test

Coding Problem