UPDATE with
WHERE
Learn how to modify specific rows in a database table using the SQL UPDATE statement combined with a WHERE clause for precise, targeted changes.
What is the UPDATE Statement?
The UPDATE statement is one of the four core SQL commands (alongside SELECT, INSERT, and DELETE). It lets you change existing data in a table: modifying one column, many columns, or even every row at once.
Why WHERE Matters
Without a WHERE clause, an UPDATE statement will change every single row in the table. The WHERE clause acts as a filter, ensuring only the rows you intend to modify are affected.
Why This Skill Matters
Every application that stores data—such as social media, banking, e-commerce, and school systems—relies on UPDATE with WHERE to make precise changes without corrupting everything else.
The Core Syntax
Anatomy of an UPDATE Statement
UPDATE vs. INSERT vs. DELETE
| UPDATE | INSERT | DELETE | |
|---|---|---|---|
| Purpose | Modify existing data | Add new rows | Remove rows |
| Uses SET? | ✓ Yes | No (uses VALUES) | No |
| Uses WHERE? | ✓ Should | No | ✓ Should |
| Row count | Same | Increases | Decreases |
Step-by-Step Approach
Example 1: Single Column Update
A student named Maya moved to a new city. Update her city from "Austin" to "Denver".
| id | name | city |
|---|---|---|
| 1 | Alex | Austin |
| 2 | Maya | Austin → Denver |
| 3 | Sam | Chicago |
Example 2: Multiple Columns
A product's name and price both need to change. Separate each assignment with a comma.
| product_id | name | price |
|---|---|---|
| 104 | Keyboard | 49.99 |
| 105 | Wireless Mouse → Wireless Mouse Pro | 19.99 → 29.99 |
| 106 | Monitor | 299.99 |
Example 3: Updating Multiple Rows
Give a 10% discount to all products priced over 50 dollars. The WHERE clause can match many rows.
| product_id | name | price |
|---|---|---|
| 104 | Keyboard | 49.99 |
| 105 | Wireless Mouse Pro | 29.99 |
| 106 | Monitor | 299.99 → 269.99 |
| 107 | Webcam | 79.99 → 71.99 |
WHERE Clause Operators
The WHERE clause supports many comparison and logical operators to target rows precisely.
Compound WHERE Conditions
Use AND and OR to combine conditions for more precise targeting.
The Danger of Forgetting WHERE
| id | name | grade |
|---|---|---|
| 1 | Alex | A → F |
| 2 | Maya | B+ → F |
| 3 | Sam | A- → F |
| 4 | Priya | B → F |
Common Mistakes
Quick Reference Summary
| Pattern | SQL Template | Effect |
|---|---|---|
| Single column, single row | SET col = val WHERE id = 1 | 1 cell changes |
| Multiple columns, single row | SET col1 = v1, col2 = v2 WHERE id = 1 | 2+ cells, 1 row |
| Single column, multiple rows | SET col = val WHERE price > 50 | 1 cell per matched row |
| Computed value | SET price = price * 0.9 WHERE ... | Uses current value |
| No WHERE (dangerous!) | SET col = val | ALL rows change |
Question 1 of 5
What does the following SQL do?
UPDATE students SET grade = 'B' WHERE name = 'Alex';
Question 2 of 5
What happens if you run UPDATE products SET price = 0; without a WHERE clause?
Question 3 of 5
Which SQL correctly updates both the city and state for employee #7?
Question 4 of 5
You want to give a $500 raise to all employees in the Marketing department who have worked for 5 or more years. Which WHERE clause is correct?
Question 5 of 5
What is the best practice before running an UPDATE statement on important data?