Lesson 20 of 30 · Changing Data
SQL DELETE: Remove Rows Safely with WHERE
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
Related rows: children first
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:
- the rows that point at the record (the "children"),
- 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, becauseDELETEremoves whole rows.- Deleting the parent first and leaving orphans behind.
ORwhere you meantAND.WHERE type = 'scroll' OR price < 50deletes every scroll and every cheap item. Read your condition out loud before you run it.
Assignment
- The cat has been found, so the quest The Missing Cat (quest
11) is cancelled. Delete its completion records first, then the quest itself. - The market only keeps receipts from 1 September 2024 onwards: delete every purchase whose
bought_ondate 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
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 .