DELETE Command
Data Manipulation Language (DML) — Remove rows from a table based on conditions.
Overview
The DELETE statement removes one or more rows from a table. A WHERE clause is used to specify which rows to delete. Without a WHERE clause, all rows in the table will be deleted.
WHERE clause unless you intentionally want to delete all rows. DELETE operations cannot be undone without a backup.
Syntax
Basic SyntaxDELETE FROM <table_name> WHERE <condition>;
WHERE clause is required in production environments. Without it, all rows will be deleted.
Basic Examples
Delete a Single Row by ID
Using the primary key for fast lookup (O(1) operation):
ExampleDELETE FROM users WHERE id = 5;
Delete by String Match
Remove rows where a text column matches a value:
ExampleDELETE FROM users WHERE name = 'Charlie';
Delete by Numeric Condition
Remove rows where a numeric column meets a condition:
ExampleDELETE FROM users WHERE age < 18;
Delete by Boolean Value
Remove inactive users:
ExampleDELETE FROM users WHERE active = FALSE;
WHERE Clause Details
Comparison Operators
The WHERE clause supports standard comparison operators:
=Equal to>Greater than<Less than>=Greater than or equal<=Less than or equal
-- Delete rows where salary is above threshold
DELETE FROM employees WHERE salary > 100000;
-- Delete rows where age is less than cutoff
DELETE FROM users WHERE age < 21;
-- Delete exact match
DELETE FROM products WHERE price = 0.00;
Complex WHERE Conditions
AND Logic
Delete rows that match multiple conditions (all must be true):
ExampleDELETE FROM users WHERE age > 30 AND active = FALSE;
OR Logic
Delete rows that match at least one condition:
ExampleDELETE FROM users WHERE status = 'archived' OR age > 100;
Combined AND/OR with Grouping
Use parentheses to control logical precedence:
ExampleDELETE FROM orders WHERE (status = 'cancelled' AND created_date < '2020-01-01')
OR (total = 0 AND reason = 'test');
NOT Operator
Delete rows that do NOT match a condition:
ExampleDELETE FROM users WHERE NOT (status = 'active');
NULL Checks
Delete Rows with NULL Values
ExampleDELETE FROM users WHERE profile_picture IS NULL;
Delete Rows WITHOUT NULL Values
ExampleDELETE FROM users WHERE phone_number IS NOT NULL;
Mixed NULL and Regular Conditions
ExampleDELETE FROM orders WHERE (delivery_date IS NULL) AND (status = 'pending');
Performance Notes
Primary Key Lookups (O(1))
Deleting by primary key is extremely fast:
FastDELETE FROM users WHERE id = 123;
Other Column Scans (O(n))
Deleting by non-indexed columns requires a full table scan:
SlowerDELETE FROM users WHERE email = 'user@example.com';
Safety Best Practices
1. Always Test First with SELECT
Before running a DELETE, verify the rows with SELECT:
Safe Approach-- First, verify what will be deleted
SELECT * FROM users WHERE age < 18;
-- Then, delete with confidence
DELETE FROM users WHERE age < 18;
2. Create a Backup
Backup your database before bulk deletions:
ExampleBACKUP 'before_deletion_2024';
DELETE FROM users WHERE status = 'deprecated';
-- Verify results, or restore if needed
3. Use Transaction Limits
Delete in smaller batches for large datasets:
Example-- Delete inactive users, but in manageable chunks
DELETE FROM users WHERE active = FALSE AND joined_date < '2020-01-01';
4. Add Comments to Risky Operations
Example-- CAUTION: Removing all test records from production database
-- Created backup: 'prod_backup_2024_01_15'
DELETE FROM orders WHERE order_id IN (9001, 9002, 9003);
Common Pitfalls
❌ Forgetting the WHERE Clause
This will delete every row in the table:
DangerousDELETE FROM users; -- ALL users deleted!
❌ Incorrect Column Names
Typos in column names will cause errors:
ErrorDELETE FROM users WHERE username = 'alice'; -- Column 'username' doesn't exist
❌ Type Mismatches
Comparing incompatible types may not match rows:
ErrorDELETE FROM users WHERE id = '123'; -- id is INT, not TEXT
❌ Complex Conditions Without Testing
Run SELECT first to verify the condition logic:
Safer-- Test the condition
SELECT COUNT(*) FROM users WHERE (age > 50 AND status = 'inactive') OR premium = FALSE;
-- Then delete
DELETE FROM users WHERE (age > 50 AND status = 'inactive') OR premium = FALSE;
DELETE vs Other DML Commands
| Command | Purpose | Can Undo? |
|---|---|---|
| INSERT | Add new rows | Use DELETE |
| SELECT | Read rows (no change) | N/A |
| UPDATE | Modify existing rows | Use UPDATE again or RESTORE |
| DELETE | Remove rows | Use RESTORE backup |