We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
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
WHEREclause! 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
- Delete the product with the lowest price
- Delete all products in the "Stationery" category
- Use DELETE with RETURNING to see what gets removed
- Try deleting everything, then reset your sandbox to get the data back!
SQL Editor
Ctrl+Enter to run
Results
Run a query to see results here
Tables: