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

  • DELETE removes rows based on a condition.
  • DELETE can be rolled back if inside a transaction.
  • TRUNCATE removes all rows instantly and cannot be rolled back in most databases.

⚡ Best Practices

  • Always back up data before using DELETE.
  • Use WHERE carefully to avoid accidental mass deletions.
  • Test your WHERE clause with SELECT first.
  • Consider TRUNCATE when 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.