SQL — Delete
Basic DELETE
DELETE FROM users
WHERE id = 1;
DELETE with conditions
DELETE FROM users
WHERE status = 'inactive';
DELETE with subquery
DELETE FROM users
WHERE id IN (
SELECT user_id FROM orders WHERE total = 0
);
DELETE with JOIN
-- MySQL
DELETE u FROM users u
JOIN orders o ON u.id = o.user_id
WHERE o.total < 10;
-- PostgreSQL
DELETE FROM users
USING orders
WHERE users.id = orders.user_id
AND orders.total < 10;
TRUNCATE vs DELETE
-- DELETE: removes rows, logs each row, can be rolled back
DELETE FROM users;
-- TRUNCATE: removes all rows, faster, minimal logging
TRUNCATE TABLE users;
DELETE returning
-- PostgreSQL
DELETE FROM users
WHERE status = 'inactive'
RETURNING id, name;
Best practices
- Always use WHERE clause with DELETE
- Back up data before mass deletes
- Use LIMIT for large deletions
- Test with SELECT first
Mini Practice
Write SQL code that:
- Deletes a specific row
- Deletes with a condition
- Uses a subquery in DELETE
- Compares DELETE and TRUNCATE
Up Next
In the next lesson, you'll learn about Order By — sorting query results.
Related Topics
Frequently Asked Questions about Delete
What is Delete in SQL?
Delete is a fundamental concept in SQL. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Delete?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Delete.
Why is Delete important in SQL?
Delete is essential for SQL development. Understanding this concept will help you write better code and solve real-world problems more effectively.