Before you start, you need
Delete one row and keep the others
A draft you no longer need does not have to be kept. DELETE removes the whole row. If you only wanted to fix the title, you would use UPDATE.
DELETE FROM drafts names the table and WHERE id = 3 chooses what to delete. The table and its columns remain, as do the rows that do not meet that condition.
Check before deleting: a SELECT with the same condition should show exactly the intended draft. Then query the remaining rows to check what was kept.
Without WHERE, every row is deleted. The drafts table in the example is set up for practice, and each attempt goes back to its starting state. In a real database, other tables may also refer to the row.
DELETE FROM table_name
WHERE condition;See it in action
We want to remove only draft 3.
Before running: If only draft 3 is deleted, which ids should remain?
SELECT id, title
FROM drafts
WHERE id = 3;
DELETE FROM drafts
WHERE id = 3;
SELECT id, title
FROM drafts
ORDER BY id;| id | title |
|---|---|
| 1 | Shipping plan |
| 2 | Copy of Shipping plan |
- The preview shows the draft you are about to remove. WHERE uses the same id for the delete.
- The final query shows that drafts 1 and 2 are still there. Draft 3 is no longer in the table.
- DELETE deletes entire rows. If you just wanted to change the title, you would use UPDATE.
- Each attempt sets up the drafts table again. You can practise the scope without deletes piling up between attempts.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Delete a reviewed draft
In drafts, query id and title for id 1, delete only that row and query the remaining rows sorted by id.
We check the full final state: the change you were asked to make and that every other row is untouched. Review the output of each statement too.
The table for this activity is already set up. Look at its data; you do not need to create it again.
Every attempt resets the data, so include all the steps you need to check in a single attempt.
View tables and data
Open this panel to see the full data.
Query the key of the target row before the change.
DELETE FROM deletes rows and WHERE limits the scope.
Use id = 1 both in the preview and in the delete.
Solution explained
SELECT id, title FROM drafts WHERE id = 1; DELETE FROM drafts WHERE id = 1; SELECT id, title FROM drafts ORDER BY id;Only id 1 is deleted; drafts 2 and 3 remain.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Delete by a condition
In the prepared drafts table, delete the copies: the drafts whose title starts with 'Copy of'. First query id and title with that same condition and, after the deletion, id and title of the remaining rows sorted by id.
We check the full final state: the change you were asked to make and that every other row is untouched. Review the output of each statement too.
The table for this activity is already set up. Look at its data; you do not need to create it again.
Every attempt resets the data, so include all the steps you need to check in a single attempt.
View tables and data
Open this panel to see the full data.
First write a SELECT with the condition and check which rows it returns.
Use exactly the same condition you checked in the DELETE.
The condition is title LIKE 'Copy of%'.
Solution explained
SELECT id, title FROM drafts WHERE title LIKE 'Copy of%'; DELETE FROM drafts WHERE title LIKE 'Copy of%'; SELECT id, title FROM drafts ORDER BY id;The preview shows only draft 2, so the DELETE with the same condition removes exactly that row. Drafts 1 and 3 are kept. The complete state is checked, not just the visible output.
Key takeaways
- DELETE removes whole rows.
- WHERE decides which ones are removed.
- Without WHERE, every row is deleted.
- The table and the rows not chosen remain.