Module 6 · Changing data

Delete rows with DELETE

By the end you will be able to delete only the planned rows and review the remaining ones.

About 12 min

0 of 52 lessons

Before you start, you need

Update rows with UPDATE

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.

Minimal syntax
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?

SQL
SELECT id, title
FROM drafts
WHERE id = 3;

DELETE FROM drafts
WHERE id = 3;

SELECT id, title
FROM drafts
ORDER BY id;
Example result: 2 rows
idtitle
1Shipping plan
2Copy of Shipping plan
  1. The preview shows the draft you are about to remove. WHERE uses the same id for the delete.
  2. The final query shows that drafts 1 and 2 are still there. Draft 3 is no longer in the table.
  3. DELETE deletes entire rows. If you just wanted to change the title, you would use UPDATE.
  4. 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

Not started

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.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Delete by a condition

Not started

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.

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.

Next: Control changes with COMMIT and ROLLBACK