SQL UPDATE
Change the values of existing rows in a table.
Basic syntax
SQL
UPDATE customers
SET city = 'Mumbai'
WHERE id = 3;
ALWAYS include WHERE
An UPDATE without WHERE updates every single row in the table. Always add a WHERE clause unless a full-table update is genuinely what you want.
Update multiple columns at once
SQL
UPDATE customers
SET city = 'Delhi',
country = 'India',
phone = '+91-9876543210'
WHERE id = 5;
Update using calculated values
SQL — apply a 10% discount
UPDATE orders
SET amount = amount * 0.90
WHERE customer_id = 1
AND order_date >= '2024-01-01';
Update based on another table (subquery)
SQL
UPDATE orders
SET amount = amount * 1.05 -- 5% markup
WHERE customer_id IN (
SELECT id FROM customers WHERE country = 'India'
);
UPDATE with JOIN (SQL Server / MySQL)
SQL Server
UPDATE o
SET o.amount = o.amount * 0.95
FROM orders o
JOIN customers c ON o.customer_id = c.id
WHERE c.city = 'London';
Safe UPDATE pattern — check first
Best practice
-- 1. Run SELECT first to confirm which rows you'll update
SELECT * FROM customers WHERE city = 'Bangalore';
-- 2. Satisfied with the results? Run the UPDATE with the same WHERE
UPDATE customers
SET country = 'India'
WHERE city = 'Bangalore';
| Clause | Role |
|---|---|
UPDATE table | Which table to modify |
SET col = val | What to change (comma-separated for multiple) |
WHERE condition | Which rows to update — always include this |