Updating Records Safely (UPDATE with WHERE)
Update targeted rows safely, preview impact, and avoid the missing-WHERE disaster.
In plain words
UPDATE changes values in rows that already exist. WHERE picks which rows; leave it out and every row changes.
Think of it like…
Correcting entries in a register with a stamp: you decide which pages to stamp (WHERE) before you start. Stamp without choosing and every page gets the correction.
Where you’ve seen it
Marking one order as shipped: UPDATE orders SET status = 'shipped', shipped_at = now() WHERE id = 501 AND status = 'paid';. The extra status condition makes the update safe if it runs twice or if the order was cancelled meanwhile.
Words you’ll hear
- SET
- The columns to change and their new values or expressions.
- WHERE
- Which rows to change. Omitting it changes all rows.
- UPDATE … FROM / JOIN
- Update rows using values from another table.
- Rows affected
- The count the database reports; check it matches what you expected.
Visual diagram
Theory & example queries
Writing the update
SET can assign constants, expressions or other columns. In standard SQL (and PostgreSQL), every expression on the right sees the row's values from before the update, so SET a = b, b = a swaps two columns. MySQL evaluates single-table assignments left to right, so the same statement sets both to b's old value, which is a classic dialect trap.
To update from another table, PostgreSQL uses UPDATE target SET … FROM source WHERE target.id = source.id, and MySQL uses UPDATE target JOIN source ON … SET …. Make sure each target row matches only one source row, or the result is unpredictable. RETURNING (PostgreSQL) shows exactly which rows changed and their new values.
| id | price |
|---|---|
| 42 | 49.5 |
| 84 | 150 |
| 11 | 12.5 |
| id | price |
|---|---|
| 42 | 39.99 |
| 84 | 159 |
| 11 | 12.5 |
Product 11 is unchanged because its staged price equals the current price.
UPDATE products pSET price = s.new_price, updated_at = now()FROM price_updates sWHERE s.product_id = p.id AND p.price IS DISTINCT FROM s.new_priceRETURNING p.id, p.price;PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
Rows returned by RETURNING2 sample rows
| id | price |
|---|---|
| 42 | 19.99 |
| 84 | 12.5 |
Note: Only products whose staged price differs from the current price are updated. Unchanged and missing staging rows do not appear in the output.
Key takeaway: SET sees old values (except in MySQL); UPDATE … FROM needs one source row per target.
Updating safely
Before an ad-hoc update, run a SELECT with exactly the same WHERE to see which rows and how many will change. Then run the UPDATE inside BEGIN … COMMIT, compare the reported row count with the preview, and ROLLBACK if it's different.
Updating millions of rows in one statement holds row locks and generates a lot of log and (in PostgreSQL) dead row versions. Update in batches by key range or with LIMIT inside a subquery, committing between batches. Adding the expected current state to WHERE (AND status = 'paid') makes updates idempotent and protects against concurrent changes.
| id | archived |
|---|---|
| 1001 | false |
| 1002 | false |
| 1003 | false |
| id | archived |
|---|---|
| 1001 | true |
| 1002 | true |
| 1003 | false |
The 2026 event remains false because it does not match the cutoff predicate.
UPDATE eventsSET archived = TRUEWHERE id IN ( SELECT id FROM events WHERE created_at < '2025-01-01' AND NOT archived LIMIT 10000 );PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
Command status per batch4 sample rows
| run | database response |
|---|---|
| 1 | UPDATE 10000 |
| 2 | UPDATE 10000 |
| 3 | UPDATE 4200 |
| 4 | UPDATE 0 |
Note: This sample starts with 24,200 eligible events. Stop when the command reports UPDATE 0, which means no remaining rows match the batch's WHERE clause.
repeated until it updates 0 rows.
Key takeaway: Preview, transaction, check the count, and batch big updates.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Preview the target rows
Run SELECT with the exact WHERE you'll use, and note the row count.
SELECT COUNT(*)FROM ordersWHERE status = 'paid' AND created_at < '2026-11-01'When to use it & trade-offs
What it is
UPDATE table SET column = value WHERE condition changes existing rows. The WHERE clause decides which rows change, so without it every row in the table is updated. SET can use expressions and the row's current values (SET stock = stock - 1), and PostgreSQL's UPDATE … FROM or MySQL's UPDATE … JOIN update rows using values from another table.
Why it matters: The missing-WHERE UPDATE is one of the most famous ways to damage production data. Safe habits (preview first, run inside a transaction, check the row count, batch big changes) are what interviewers and senior colleagues look for.
When to reach for it
- Changing statuses and fields in response to user actions
- Applying bulk corrections from a staging table
- Backfilling a new column in batches
When to avoid or be careful
- UPDATE without WHERE (unless you truly mean every row)
- Manual production updates outside a transaction
- Updating millions of rows in a single statement on a busy table
Single UPDATE vs UPDATE … FROM vs batched updates
Strengths: One statement, atomic.
Watch out: Huge updates lock many rows and bloat tables.
Strengths: Set-based, no application loop.
Watch out: Multiple matching source rows give unpredictable results.
Strengths: Short locks, steady load, restartable.
Watch out: Not atomic as a whole; needs a loop or job.
Interview signals & numbers to know
When this topic comes up
- “I accidentally updated every row”
- “Update prices from this spreadsheet”
- Backfills
- UPDATE with JOIN
- Idempotent operations
Benchmarks & rules of thumb
- 0
- rows protected when WHERE is missing
- 10,000
- a common batch size for large updates
- 1
- source row allowed per target in UPDATE … FROM
Common mistakes & follow-ups
Common interview mistakes
- Running UPDATE users SET email_verified = true and verifying every user.
- Writing SET a = b, b = a in MySQL and expecting a swap.
- Joining to a source table with duplicate keys in UPDATE … FROM, so the value applied is arbitrary.
Likely interviewer follow-ups
- How do you update a table using values from another table?
- How would you safely run a manual UPDATE in production?
- How would you update 50 million rows without locking the table for hours?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Updating Records Safely (UPDATE with WHERE) with hands-on SQL practice and interview Q&A.