MySQL — Delete
Basic DELETE
DELETE FROM users WHERE id = 1;
Delete Multiple Rows
DELETE FROM users WHERE age < 18;
Delete All Rows
DELETE FROM users;
DELETE Examples
-- Delete single row
DELETE FROM users WHERE id = 5;
-- Delete with multiple conditions
DELETE FROM orders
WHERE status = 'cancelled' AND created_at < '2023-01-01';
-- Delete with LIMIT
DELETE FROM logs
WHERE created_at < '2022-01-01'
LIMIT 1000;
-- Delete from multiple tables
DELETE users, orders
FROM users
INNER JOIN orders ON users.id = orders.user_id
WHERE users.is_active = FALSE;
WARNING
-- Always use WHERE clause!
DELETE FROM users; -- Deletes ALL rows!
-- Test with SELECT first
SELECT * FROM users WHERE condition;
Best Practices
- Always use WHERE clause
- Test with SELECT first
- Use LIMIT for large deletions
- Consider soft deletes (is_active flag)
- Backup before bulk deletes
Mini Practice
Write MySQL code that:
- Deletes a single row
- Deletes multiple rows
- Uses LIMIT for deletion
- Tests with SELECT first
Up Next
Next: Learn about Limiting Results.
Related Topics
Frequently Asked Questions about Delete
What is Delete in MySQL?
Delete is a fundamental concept in MySQL. 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 MySQL?
Delete is essential for MySQL development. Understanding this concept will help you write better code and solve real-world problems more effectively.