Skip to content
Codewilds

Lesson 20 of 30 · Changing Data

SQL DELETE: Remove Rows Safely with WHERE

easy10 XP2 min read

Records don't live forever: an order is cancelled, an account is closed, old logs are cleaned up. DELETE removes rows from a table. Like UPDATE, it's only as safe as its WHERE clause.

DELETE FROM … WHERE

DELETE FROM items
WHERE type = 'scroll' AND price < 50;

Every row that matches the WHERE is removed (here only the Scroll of Return, at 45 gold) and all other rows stay. The editor reports how many rows were deleted: 1 row changed.

DELETE always removes whole rows, so there's no column list. And without a WHERE, it removes all of them: DELETE FROM items; leaves an empty items table. The table itself survives; removing a table completely is DROP TABLE, which you'll meet in the next chapter.

Look before you delete

A deleted row is gone. So before you delete, run the same condition as a SELECT, check the rows, and only then swap SELECT * for DELETE:

SELECT * FROM purchases WHERE hero_id = 9;   -- 1. look: one purchase
DELETE FROM purchases WHERE hero_id = 9;     -- 2. same WHERE, now delete

completions and purchases point at heroes, quests and items through their ids (hero_id, quest_id, item_id). If you delete a hero but keep their purchases, those purchases point at a hero who no longer exists. Such orphans silently vanish from joins and make reports wrong. So delete in this order:

  1. the rows that point at the record (the "children"),
  2. then the record itself (the "parent").
DELETE FROM purchases WHERE hero_id = 9;   -- Iris Dawnpetal's purchases
DELETE FROM heroes WHERE id = 9;           -- then Iris herself

In the next chapter you'll see how foreign keys make the database enforce this order for you.

The tables in this lesson:

Table Columns
quests id, title, region, difficulty, reward
completions hero_id, quest_id, completed_on (one row per hero per finished quest)
purchases id, hero_id, item_id, quantity, bought_on (date as text, 'YYYY-MM-DD')

Common mistakes

  • No WHERE. DELETE FROM purchases; deletes all 26 purchases.
  • DELETE * FROM … is a syntax error (near "*"). There's nothing to list, because DELETE removes whole rows.
  • Deleting the parent first and leaving orphans behind.
  • OR where you meant AND. WHERE type = 'scroll' OR price < 50 deletes every scroll and every cheap item. Read your condition out loud before you run it.

Assignment

  1. The cat has been found, so the quest The Missing Cat (quest 11) is cancelled. Delete its completion records first, then the quest itself.
  2. The market only keeps receipts from 1 September 2024 onwards: delete every purchase whose bought_on date is before '2024-09-01'.

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 20 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.