Skip to content
Codewilds

Lesson 19 of 30 · Changing Data

SQL UPDATE: Change Existing Rows with SET and WHERE

easy10 XP2 min read

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:

  1. Write the SELECT first: SELECT * FROM heroes WHERE guild_id = 2;
  2. Check that it returns exactly the rows you mean.
  3. Turn it into the UPDATE, keeping the same WHERE.

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

  • AND instead of a comma. SET guild_id = 4 AND level = 10 is valid SQL, but it means guild_id = (4 AND level = 10), a true/false value. The guild becomes 0 and the level doesn't change at all.
  • = NULL in a condition. WHERE guild_id = NULL matches nothing; write WHERE guild_id IS NULL. In SET, guild_id = NULL is fine, because there it's an assignment.
  • Forgetting the WHERE. See above. Write it before you write the SET.

Assignment

Write three UPDATE statements:

  1. Dax Shadowstep (hero 4) joins the Night Market (guild 4) and reaches level 10. Change both columns in one UPDATE. His gold stays the same.
  2. The Iron Wall pays a bonus: every hero in guild 2 gets 100 more gold. Use an expression, not fixed numbers.
  3. 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
Play the 7 free lessons

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 .

Write code ↓
SQL+10 XP

Lesson 19 is part of the full course. The first 7 lessons are free.

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
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.