SQL UPDATE Statement
Chapter 10 of 40 · Estimated read time: 7 min
The UPDATE statement changes the value of one or more columns in existing rows. It always works with a SET clause (what to change) and — almost always — a WHERE clause (which rows to change).
⚠️ The #1 SQL mistake: Running UPDATE without a WHERE clause updates every single row in the table. Before running any UPDATE, always ask: "which specific rows should change?" Confirm by running a SELECT with the same WHERE first.
Syntax
UPDATE table_name
SET column1 = value1,
column2 = value2
WHERE condition;
Safe Update Workflow
Before every UPDATE, run a SELECT with the same WHERE clause to preview exactly which rows will change:
-- Step 1: Preview — which rows will be affected? SELECT product_id, product_name, price FROM products WHERE product_id = 1; -- Shows: Wireless Mouse, price = 899 -- Step 2: Run the UPDATE only after confirming UPDATE products SET price = 999 WHERE product_id = 1; -- Step 3: Verify the change SELECT product_id, product_name, price FROM products WHERE product_id = 1; -- Shows: Wireless Mouse, price = 999
Example 1 — Update a Single Column
-- A customer moved cities — update their city UPDATE customers SET city = 'Gurugram' WHERE customer_id = 'C04';
BEFORE UPDATE
| customer_id | customer_name | city |
|---|---|---|
| C04 | Sneha Gupta | Noida |
AFTER UPDATE
| customer_id | customer_name | city |
|---|---|---|
| C04 | Sneha Gupta | Gurugram |
Example 2 — Update Multiple Columns at Once
-- Update both price AND stock for a product
UPDATE products
SET price = 1299,
stock = 200
WHERE product_id = 7; -- HDMI CableUpdating multiple columns in one statement is more efficient than running two separate UPDATE statements — the change happens in a single transaction.
Example 3 — Update Using a Calculation
You can reference the current column value in the calculation — useful for applying percentage changes:
-- Apply a 10% price increase to all Furniture products UPDATE products SET price = price * 1.10 WHERE category = 'Furniture'; -- Verify: what are the new prices? SELECT product_name, category, price FROM products WHERE category = 'Furniture';
AFTER 10% price increase
| product_name | category | price |
|---|---|---|
| Desk Lamp | Furniture | 1318.9 |
| Ergonomic Chair | Furniture | 9898.9 |
Example 4 — Update Multiple Rows Matching a Condition
-- All Electronics products are out of stock — update stock to 0 UPDATE products SET stock = 0 WHERE category = 'Electronics'; -- This updates 3 rows (Wireless Mouse, USB-C Hub, HDMI Cable) at once
Example 5 — Update to NULL (Clear a Value)
-- Remove a customer's email (they requested data deletion) UPDATE customers SET email = NULL WHERE customer_id = 'C03';
Example 6 — Update Based on Another Table
Use a subquery in the WHERE clause to update rows based on data from another table:
-- Mark all orders from customers in Noida as "priority" -- (assumes an order_flag column exists) UPDATE orders SET order_flag = 'priority' WHERE customer_id IN ( SELECT customer_id FROM customers WHERE city = 'Noida' );
What Happens Without WHERE
-- ❌ DANGEROUS — updates EVERY row in the table UPDATE products SET stock = 0; -- This sets stock to 0 for Wireless Mouse, USB-C Hub, Notebook, Pens, -- Desk Lamp, Ergonomic Chair, and HDMI Cable — all 7 products. -- There is no automatic undo in most databases.
Always run in a transaction when possible:
BEGIN; — starts the transactionUPDATE ...; — run your updateSELECT * FROM ... WHERE ...; — verify it looks rightCOMMIT; — make it permanent, OR ROLLBACK; to undo✏️ Practice Exercises
1. Increase the price of all Stationery products by ₹10.
2. Update customer C02's email to 'priya.new@example.com'.
3. Set stock to 500 for all products supplied by S02.
Show Answers
-- 1. UPDATE products SET price = price + 10 WHERE category = 'Stationery'; -- 2. UPDATE customers SET email = 'priya.new@example.com' WHERE customer_id = 'C02'; -- 3. UPDATE products SET stock = 500 WHERE supplier_id = 'S02';