SQL DELETE Statement
beginnerbasics
2 min read
What is the DELETE Statement?
The DELETE statement removes rows from a table. Like UPDATE, it uses a WHERE clause to target specific rows.
Syntax
DELETE FROM table_name
WHERE condition;To delete all rows:
DELETE FROM table_name;When to Use
- Removing a cancelled order
- Cleaning up test data
- Purging records older than a certain date
Key Points
- Always Use WHERE — Without
WHERE, all rows are deleted. UseTRUNCATEif you truly want to remove everything, as it is faster. - Foreign Keys — If other tables reference the row via a foreign key, the delete may fail unless
ON DELETE CASCADEis set. - RETURNING —
DELETE FROM students WHERE id = 5 RETURNING *;returns the deleted row. - Transactions — Wrap deletes in a transaction (
BEGIN; ... COMMIT;) for safety. - Soft Deletes — Many applications prefer setting a
deleted_attimestamp rather than actually removing rows.
Guided Practice
Solve the challenge below. Use hints when stuck and check your answer for instant feedback.
Practice challengeGuided learning mode
SQL DELETE Statement Challenge
Write a query that solve this task: remove the order with id 42.
Expected result
One row deleted from the orders table.
Hidden checks
- Returned rows and values
- Output columns and result shape
- Final database state after the query runs
Lesson guidance
What is the DELETE Statement?
Initializing database...Each run starts from fresh sample data.
More Examples
Delete with RETURNING
Delete old enrollments and return the removed rows.
Initializing database...Each run starts from fresh sample data.
Frequently Asked Questions
What is the difference between DELETE and TRUNCATE?
DELETE removes rows one by one and supports WHERE. TRUNCATE removes all rows at once, is faster, and resets auto-increment counters. TRUNCATE cannot be used with WHERE.
Can I undo a DELETE?
Only if the DELETE was inside a transaction that has not been committed. After COMMIT, the deletion is permanent.