Ira SQL ProIra SQL Pro

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

  1. Always Use WHERE — Without WHERE, all rows are deleted. Use TRUNCATE if you truly want to remove everything, as it is faster.
  2. Foreign Keys — If other tables reference the row via a foreign key, the delete may fail unless ON DELETE CASCADE is set.
  3. RETURNINGDELETE FROM students WHERE id = 5 RETURNING *; returns the deleted row.
  4. Transactions — Wrap deletes in a transaction (BEGIN; ... COMMIT;) for safety.
  5. Soft Deletes — Many applications prefer setting a deleted_at timestamp 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.

Related Topics