01 / 21

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.

SQL

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.

UPDATE
Changes values in existing rows
WHERE
Filters which rows get changed
SET
Specifies new column values

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.

Without WHERE Row 1 ← changed Row 2 ← changed Row 3 ← changed Row 4 ← changed ALL rows affected! With WHERE Row 1 — untouched Row 2 ← changed Row 3 — untouched Row 4 — untouched Only 1 row 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.

E-Commerce
Update a product's price without changing every product in the catalog
School Systems
Change one student's grade without overwriting the entire class
Banking
Update an account balance for one customer, not every customer

The Core Syntax

UPDATE table_name SET column1 = new_value1, column2 = new_value2 WHERE condition;
1
UPDATE: names the table you want to modify
2
SET: lists the columns and their new values
3
WHERE: filters which rows are affected

Anatomy of an UPDATE Statement

UPDATE students SET grade = 'A' WHERE student_id = 42; Which table? What to change? Which rows?

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

1
Identify the table: which table holds the data you need to change?
2
Write a SELECT first: run SELECT * FROM table WHERE condition; to verify which rows match
3
Write the SET clause: list each column = new_value, separated by commas
4
Add the WHERE clause: reuse the same condition from your SELECT
5
Execute and verify: run the UPDATE, then SELECT again to confirm the change

Example 1: Single Column Update

A student named Maya moved to a new city. Update her city from "Austin" to "Denver".

UPDATE students SET city = 'Denver' WHERE name = 'Maya';
students table (after)
idnamecity
1AlexAustin
2MayaAustin → Denver
3SamChicago

Example 2: Multiple Columns

A product's name and price both need to change. Separate each assignment with a comma.

UPDATE products SET name = 'Wireless Mouse Pro', price = 29.99 WHERE product_id = 105;
products table (after)
product_idnameprice
104Keyboard49.99
105Wireless Mouse → Wireless Mouse Pro19.99 → 29.99
106Monitor299.99

Example 3: Updating Multiple Rows

Give a 10% discount to all products priced over 50 dollars. The WHERE clause can match many rows.

UPDATE products SET price = price * 0.90 WHERE price > 50;
products table (after)
product_idnameprice
104Keyboard49.99
105Wireless Mouse Pro29.99
106Monitor299.99 → 269.99
107Webcam79.99 → 71.99

WHERE Clause Operators

The WHERE clause supports many comparison and logical operators to target rows precisely.

=
Exact match: WHERE city = 'Denver'
> < >= <=
Comparisons: WHERE price > 50
!= or <>
Not equal: WHERE status != 'active'
AND / OR
Combine conditions: WHERE age > 18 AND city = 'Austin'
IN (...)
Match a list: WHERE id IN (1, 3, 5)
LIKE
Pattern match: WHERE name LIKE 'A%'

Compound WHERE Conditions

Use AND and OR to combine conditions for more precise targeting.

-- Give bonus to senior employees in Sales UPDATE employees SET salary = salary + 5000 WHERE department = 'Sales' AND years_employed >= 10;
-- Deactivate accounts with no email OR no phone UPDATE accounts SET status = 'inactive' WHERE email IS NULL OR phone IS NULL;

The Danger of Forgetting WHERE

-- DANGER: This changes EVERY student's grade! UPDATE students SET grade = 'F';
students table (catastrophic result)
idnamegrade
1AlexA → F
2MayaB+ → F
3SamA- → F
4PriyaB → F

Common Mistakes

✕ Missing WHERE
Forgetting WHERE updates every row in the table. Always double-check before executing.
✕ AND in SET
Writing SET col1 = val1 AND col2 = val2 instead of using a comma. AND is for WHERE, commas are for SET.
✕ Wrong Quotes
Forgetting quotes around string values: SET city = Denver instead of SET city = 'Denver'.
✕ = vs IS for NULL
Writing WHERE col = NULL instead of WHERE col IS NULL. NULL requires IS, not equals.
✕ Wrong Column Name
Misspelling a column name causes an error. Always verify column names with a SELECT first.
✕ No Backup Plan
Running UPDATE on production data without a transaction or backup. Use BEGIN and ROLLBACK to test safely.

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?