SQL DELETE Statement
The DELETE statement in SQL is used to remove existing rows from a table. You can delete specific rows using the WHERE clause, or remove all rows if the clause is omitted.
📌 Syntax
DELETE FROM table_name WHERE condition;
⚠️ Important: If you omit the WHERE clause, all rows in the table will be deleted!
📊 Example Table: users
id | name | age | city ---+---------+-----+--------- 1 | Alice | 22 | New York 2 | Bob | 30 | London 3 | Charlie | 18 | Sydney 4 | Diana | 27 | Toronto 5 | Ethan | 35 | Paris
🔍 Example 1: Delete a Single Row
DELETE FROM users WHERE name = 'Charlie';
🔍 Example 2: Delete Multiple Rows
Remove all users younger than 21:
DELETE FROM users WHERE age < 21;
🔍 Example 3: Delete All Rows
Running this will wipe the table completely (but the table structure stays intact):
DELETE FROM users;
🔍 Example 4: Delete with Complex Condition
Remove all users from Paris or Berlin:
DELETE FROM users
WHERE city IN ('Paris', 'Berlin');🔍 Example 5: Difference Between DELETE and TRUNCATE
DELETEremoves rows based on a condition.DELETEcan be rolled back if inside a transaction.TRUNCATEremoves all rows instantly and cannot be rolled back in most databases.
⚡ Best Practices
- Always back up data before using
DELETE. - Use
WHEREcarefully to avoid accidental mass deletions. - Test your
WHEREclause withSELECTfirst. - Consider
TRUNCATEwhen you need to clear a table completely.
📝 Summary
The DELETE statement is used to remove one or more rows from a table. Without WHERE, it will delete every row. Always test your queries before executing them on real data to prevent accidental data loss.
🚀 Next Steps
In the next lesson, we will learn about the SQL SELECT Statement, the most commonly used command in SQL, which retrieves data from tables.