SQL DML
SQL UPDATE
Modify existing records in a database table using the UPDATE statement.
The SQL UPDATE Statement
The UPDATE statement is used to modify the existing records in a table.
ALWAYS use WHERE with UPDATE!
If you omit the WHERE clause, ALL records will be updated! Always double-check your WHERE clause before executing an UPDATE.
Updating Multiple Columns
You can update multiple columns in a single UPDATE statement by separating them with commas.
UPDATE with Expressions
You can use expressions to compute new values based on existing column values.
Returning Updated Rows
Some databases (like PostgreSQL) support a RETURNING clause to see the updated records.
Example
sql
-- Update a single column
UPDATE users
SET email = 'newemail@example.com'
WHERE id = 42;
-- Update multiple columns
UPDATE products
SET
price = 29.99,
updated_at = NOW(),
in_stock = true
WHERE id = 101;
-- Update with expression
UPDATE products
SET price = price * 0.9 -- 10% discount
WHERE category = 'sale';
-- Update with subquery
UPDATE orders
SET status = 'shipped'
WHERE user_id IN (
SELECT id FROM users WHERE vip = true
);
-- Update all rows (intentional!)
UPDATE settings SET maintenance_mode = false;
-- RETURNING (PostgreSQL)
UPDATE users
SET last_login = NOW()
WHERE id = 42
RETURNING id, name, last_login;
-- Safe pattern: preview first
SELECT * FROM users WHERE id = 42;
-- Then update
UPDATE users SET role = 'admin' WHERE id = 42;Want to run this code interactively?
Try in Compiler