DELETE - Removing Data

The DELETE statement removes rows from a table.

Basic Syntax

DELETE FROM table_name
WHERE condition;

Critical: Like UPDATE, always include a WHERE clause! Without it, you'll delete every row.

Deleting Specific Rows

-- Delete a specific product
DELETE FROM products
WHERE name = 'USB Cable';

-- Delete all out-of-stock products
DELETE FROM products
WHERE in_stock = false;

DELETE with RETURNING

See what was deleted:

DELETE FROM products
WHERE quantity = 0
RETURNING name, category;

Deleting All Rows

If you truly want to remove all rows:

-- Option 1: DELETE (logged, slower, can be rolled back in a transaction)
DELETE FROM products;

-- Option 2: TRUNCATE (faster, resets sequences)
TRUNCATE products;

Safe Deletion Pattern

Always check before you delete:

-- Step 1: See what will be deleted
SELECT * FROM products WHERE price < 10;

-- Step 2: Delete if it looks right
DELETE FROM products WHERE price < 10;

DELETE vs TRUNCATE

Feature DELETE TRUNCATE
Can use WHERE Yes No
Speed Slower (row-by-row) Very fast
Triggers Fires row triggers Does not fire row triggers
RETURNING Supported Not supported
Reset sequences No Optional (RESTART IDENTITY)

Don't Panic!

In this sandbox, you can always reset your data using the "Reset Sandbox" button. So feel free to experiment with DELETE - you can't break anything permanently!

Try It Yourself

  1. Delete the product with the lowest price
  2. Delete all products in the "Stationery" category
  3. Use DELETE with RETURNING to see what gets removed
  4. Try deleting everything, then reset your sandbox to get the data back!
Take Quiz Next Lesson
SQL Editor Ctrl+Enter to run
Results
Run a query to see results here
Tables: