DML: DELETE

The DELETE statement in SQL is used to delete existing records from a table.

Basic Syntax

DELETE FROM table_name
WHERE condition;
  • table_name: The table to delete records from.
  • condition: An optional clause to filter rows to delete.

It’s very important to always use the WHERE clause in a DELETE statement. Without it, all records in the table will be deleted.

Example 1 - Basic DELETE

For the “customer” table:

customer_idnamecountry
1JohnUSA
2MichaelGermany
3SarahFrance
4SallyUK

If we want to delete Sally’s record from the table, we can use the following SQL:

DELETE FROM customer
WHERE name = 'Sally';

The “customer” table would now look like this:

customer_idnamecountry
1JohnUSA
2MichaelGermany
3SarahFrance

Example 2 - Delete All Rows

If we want to delete all rows in a table, we can do so without the WHERE clause. However, be careful because this will delete all rows in the table.

DELETE FROM product;

This will delete all rows in the “product” table.

Further Readings

  1. SQL DELETE Statement - W3Schools
  2. The SQL DELETE Statement - SQL Tutorial
  3. Delete Data In a MySQL Table Using MySQLi and PDO - W3Schools
  4. SQL DELETE JOIN Statement - MySQL Tutorial
  5. SQL Server DELETE Statement - SQL Server Tutorial
  6. DELETE (Transact-SQL) - Microsoft Docs
Last updated on