UPDATE
What it means
Section titled βWhat it meansβ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.
Examples
Section titled βExamplesβUPDATE Customers SET Email = 'newemail@example.com' WHERE Id = 42;
-- Update multiple columns in one statementUPDATE CustomersSET Email = 'alice.new@example.com', Phone = '555-0100'WHERE Id = 42;
-- Update based on a calculation involving the current valueUPDATE Products SET Price = Price * 1.10 WHERE Category = 'Electronics';
-- Update using a JOIN (SQL Server-specific syntax)UPDATE oSET o.Status = 'Shipped'FROM Orders oINNER JOIN Shipments s ON o.Id = s.OrderIdWHERE s.ShippedDate IS NOT NULL;Common mistake
Section titled βCommon mistakeβ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 droppedUPDATE Customers SET Email = 'alice@example.com'; -- overwrites EVERY customer's email!
-- CorrectUPDATE Customers SET Email = 'alice@example.com' WHERE Id = 42;
-- Safe habit: run the SELECT first to confirm exactly which rows would be affectedSELECT * FROM Customers WHERE Id = 42; -- verify, THEN run the UPDATEQuick practice
Section titled βQuick practiceβ-
What happens if you run
UPDATE Products SET Price = 0;with noWHEREclause?Answer
Every row'sPriceis set to0β the update applies to the entire table. -
What does
UPDATE Products SET Price = Price * 1.10do?Answer
Increases every matched row'sPriceby 10%, using each row's own current value as the basis for the calculation. -
Whatβs a safe habit before running an
UPDATEwith aWHEREclause on production data?Answer
Run the equivalentSELECTwith the sameWHEREclause first, to confirm exactly which rows would be affected before actually updating them.