SQL · Chapter 8 of 42
SQL UPDATE
UPDATE modifies existing rows. ALWAYS include a WHERE clause — without one, EVERY row is updated.
`UPDATE table SET col = value WHERE condition;`
Syntax
UPDATE table SET col = value WHERE condition;Multiple columns
`SET col1 = val1, col2 = val2, ...`
Using existing values
You can reference other columns: `UPDATE products SET price = price * 1.1;`
Example 1 (sql)
UPDATE users SET age = 26 WHERE name = 'Ana';Output
1 row updatedChange Ana's age.
Example 2 (sql)
UPDATE products SET price = price * 1.10 WHERE category = 'Books';Output
3 rows updatedBump prices by 10% for one category.
Key points
- Missing WHERE = update ALL rows.
- SET multiple columns with commas.
- Can reference existing column values.
- Wrap in a transaction to allow rollback.
💡 Note: Before running an UPDATE, run the same WHERE clause as a SELECT to preview the affected rows.
