Q1Which correctly describes the roles of SET and WHERE in an UPDATE statement?
UPDATE and DELETE — Updating and Deleting Rows
Learn SQL UPDATE and DELETE from the basics. Cover multi-column updates with SET, narrowed deletes with WHERE, and what happens when you forget WHERE — all running live in your browser.
UPDATE — rewriting existing rows
INSERT was for "adding new rows." UPDATE is the operation for rewriting the values of rows that already exist. Changing a member's status, revising a product's price, updating a purchase count — UPDATE is part of everyday operations in any production app.
The basic form is UPDATE table_name SET col = val WHERE condition;. SET says "which column gets which value," and WHERE says "which rows are affected."
-- Rewrite the status of customer where id = 1 to 'active'
UPDATE customer SET status = 'active' WHERE id = 1;
-- Check the result
SELECT * FROM customer;
UPDATE table_name is the target table, SET col = val is the change, and WHERE condition narrows down which rows. Get a feel for the position and role of each.Forgetting WHERE updates every row
Drop the WHERE from UPDATE customer SET status = 'active' WHERE id = 1; (becoming UPDATE customer SET status = 'active';) and every row of customer has its status rewritten to 'active'. There's no error, which is exactly what makes this an easy way to silently corrupt production data.
Updating multiple columns at once
By listing `col = val` pairs separated by commas in the SET clause, you can update multiple columns at once: SET col1 = val1, col2 = val2, ....
The right-hand side can also be an expression that includes the column itself. SET price = price + 100 means "rewrite the target row's price to its current price plus 100."
col = val pair or several separated by commas, and the right-hand side can also be an expression.-- Update multiple columns to absolute values at once
UPDATE inventory SET price = 200, stock = 100 WHERE id = 2;
-- Update with expressions (referencing existing values)
UPDATE inventory SET price = price + 100, stock = stock - 10 WHERE id = 2;
DELETE — removing rows
DELETE is the operation for removing rows from a table. The basic form is DELETE FROM table_name WHERE condition;, with WHERE narrowing down the target rows. Unlike SELECT, there's no column list (SELECT col1, col2) — DELETE removes whole rows.
The table definition (the column structure) sticks around, so you can INSERT into the same table after deleting. To remove the table itself, use DROP TABLE / TRUNCATE (covered in the next article).
DELETE FROM table_name is the target table; WHERE condition decides which rows to remove. There's no equivalent of SELECT's column list or UPDATE's SET clause in DELETE.-- Delete all expired subscriptions (status = 'expired')
DELETE FROM subscription WHERE status = 'expired';
-- Check the result (only the surviving rows remain)
SELECT * FROM subscription;
Watch out for missing WHERE — the all-rows update/delete trap
Forgetting WHERE is the most common accident with UPDATE and DELETE. Without WHERE, the syntax is still legal — and the target expands to every row in the table. UPDATE table SET col = val; rewrites every row to the same value; DELETE FROM table; empties the table.
Even if you notice immediately, you can't undo it after the change is committed. The basic operational rule in production is always run the same WHERE as a `SELECT` first to visually confirm the target rows before rewriting.
In production, confirm target rows with SELECT first
For UPDATE / DELETE on a production database, run the same WHERE as a SELECT first and visually confirm the target rows are what you expect before flipping it into the rewrite. The two-step procedure:
1. Run SELECT * FROM table_name WHERE condition; to confirm the target rows (does the row count and the values match what you expected?)
2. If everything looks right, switch the same WHERE into UPDATE table_name SET col = val WHERE condition; or DELETE FROM table_name WHERE condition; and execute
Since the WHERE clause is reused via copy-paste, this also reduces the kind of accident where WHERE gets accidentally dropped during the rewrite.
Knowledge Check
Answer each question one by one.
Q2Which is the correct way to update both name and email for the row in customer where id = 1 at the same time?
Q3What happens when you run DELETE FROM product; without WHERE?