SQL from Zero › Module 3: DML: filling and changing data

Removing rows with DELETE

Remove the rows you don't need, and only those.

Lesson 15 of 33 · about 10 minutes

Video: Clearing the shelf, carefullyThe narrated video for this lesson is coming soon. The lesson below covers the same ideas.

The lesson

DELETE removes whole rows from a table.

DELETE FROM orders
WHERE status = 'cancelled';

Just like UPDATE, the WHERE decides which rows are affected, and just like UPDATE, forgetting it is a disaster: DELETE FROM orders; removes every order. Use the same habit: SELECT first, then DELETE.

DELETE vs DROP. These are easy to mix up:

CommandFamilyWhat goesWhat stays
DELETE FROM books WHERE ...DMLThe matching rowsThe table and other rows
DELETE FROM booksDMLEvery rowThe empty table
DROP TABLE booksDDLThe table and all rowsNothing

At many companies, important rows are never deleted at all. Instead a column like is_active is set to 0 with an UPDATE. This is called a soft delete, and it means mistakes can be undone and history is kept.

Key ideas
  • DELETE FROM table WHERE condition removes matching rows.
  • Without WHERE, DELETE removes every row. Check with SELECT first.
  • DELETE removes rows; DROP TABLE removes the whole table. Many teams prefer soft deletes.

Practise

Loading practice…

Quick check

1. What does DELETE FROM customers; do?

2. Which is a soft delete?

3. DELETE belongs to which family?