Lesson 19 of 30 · Changing Data
SQL UPDATE: Change Existing Rows with SET and WHERE
Data changes all the time: heroes level up, prices rise, players change their email address. UPDATE changes the values in rows that already exist, and it's only as safe as its WHERE clause.
UPDATE … SET … WHERE
UPDATE heroes
SET level = 10
WHERE id = 4;
Read it as: in heroes, set level to 10 in the rows where id = 4. The WHERE works exactly like in a SELECT: every matching row changes and every other row is left alone. One hero matches here, so the editor reports 1 row changed.
To change several columns at once, separate the assignments with commas:
UPDATE heroes
SET class = 'Ranger', guild_id = 1
WHERE name = 'Kira Frostvale';
Updating with expressions
The right side of = can be any expression, and it can use the row's current values:
UPDATE items
SET price = price * 2
WHERE rarity = 'legendary';
For each legendary item, SQLite reads the old price, doubles it and writes the result back: Dragonscale Mail goes from 1500 to 3000 and Crown of Stars from 2500 to 5000. level = level + 1 and name = name || ' the Brave' work the same way.
The missing WHERE
WHERE is optional, and that's the danger:
UPDATE heroes SET gold = 0; -- 15 rows changed: everyone is broke
Without a WHERE, UPDATE changes every row in the table, and there is no undo. Here every run starts from a fresh database; in a real one, that's an outage. A safe habit:
- Write the
SELECTfirst:SELECT * FROM heroes WHERE guild_id = 2; - Check that it returns exactly the rows you mean.
- Turn it into the
UPDATE, keeping the sameWHERE.
The columns you'll need:
| Table | Columns |
|---|---|
heroes |
id, name, class, level, gold, guild_id, joined |
items |
id, name, type ('weapon', 'armor', 'potion' or 'scroll'), price, rarity |
Common mistakes
ANDinstead of a comma.SET guild_id = 4 AND level = 10is valid SQL, but it meansguild_id = (4 AND level = 10), a true/false value. The guild becomes0and the level doesn't change at all.= NULLin a condition.WHERE guild_id = NULLmatches nothing; writeWHERE guild_id IS NULL. InSET,guild_id = NULLis fine, because there it's an assignment.- Forgetting the
WHERE. See above. Write it before you write theSET.
Assignment
Write three UPDATE statements:
- Dax Shadowstep (hero
4) joins the Night Market (guild4) and reaches level10. Change both columns in oneUPDATE. His gold stays the same. - The Iron Wall pays a bonus: every hero in guild
2gets 100 more gold. Use an expression, not fixed numbers. - The market raises potion prices: every item of type
'potion'costs 5 more.
Premium lesson
Unlock the full Learn SQL course
- All 30 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Keep learning
Need a quick reminder? The SQL cheat sheet has every topic of Learn SQL on one page.
Lesson last updated on .