Skip to content

UPDATE

UPDATE modifies existing rows in a table, setting one or more columns to new values, optionally restricted by a WHERE clause. Without WHERE, it updates every row in the table β€” the same β€œforgot the filter” risk as DELETE.

UPDATE Customers SET Email = 'newemail@example.com' WHERE Id = 42;
-- Update multiple columns in one statement
UPDATE Customers
SET Email = 'alice.new@example.com', Phone = '555-0100'
WHERE Id = 42;
-- Update based on a calculation involving the current value
UPDATE Products SET Price = Price * 1.10 WHERE Category = 'Electronics';
-- Update using a JOIN (SQL Server-specific syntax)
UPDATE o
SET o.Status = 'Shipped'
FROM Orders o
INNER JOIN Shipments s ON o.Id = s.OrderId
WHERE s.ShippedDate IS NOT NULL;

Running UPDATE TableName SET Column = Value; without a WHERE clause, intending to update one row but forgetting the filter β€” this silently overwrites that column for every row in the table.

-- Intended to fix one customer's email, but the WHERE clause got dropped
UPDATE Customers SET Email = 'alice@example.com'; -- overwrites EVERY customer's email!
-- Correct
UPDATE Customers SET Email = 'alice@example.com' WHERE Id = 42;
-- Safe habit: run the SELECT first to confirm exactly which rows would be affected
SELECT * FROM Customers WHERE Id = 42; -- verify, THEN run the UPDATE
  1. What happens if you run UPDATE Products SET Price = 0; with no WHERE clause?

    AnswerEvery row's Price is set to 0 β€” the update applies to the entire table.
  2. What does UPDATE Products SET Price = Price * 1.10 do?

    AnswerIncreases every matched row's Price by 10%, using each row's own current value as the basis for the calculation.
  3. What’s a safe habit before running an UPDATE with a WHERE clause on production data?

    AnswerRun the equivalent SELECT with the same WHERE clause first, to confirm exactly which rows would be affected before actually updating them.