Lessons · SQL · UPDATE needs WHERE
UPDATE needs a WHERE
UPDATE table SET col = value WHERE condition changes matching rows. Without WHERE, every row.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
A sale that reprices the whole catalogue, a status change that marks every ticket closed.
How to think about it
How many rows should this change? SELECT with the WHERE first, count the rows, then UPDATE.
Worked example
SELECT COUNT(*) FROM films WHERE year < 1950;How many will change.
UPDATE films SET era = 'classic' WHERE year < 1950;Same WHERE.
Your turn
Mark one order shipped.
UPDATE orders SET status = 'shipped' id = 42;
Run a query against real tables
The trap
SET col = value, col2 = value2 uses commas, not AND. AND there is a boolean expression and does something surprising.
Practise UPDATE needs WHERE on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.