The DELETE statement is employed to remove existing records from a table.
DELETE FROM table_name WHERE condition; |
Note: Exercise caution when deleting records from a table! Pay attention to the WHERE clause in the DELETE statement, as it specifies which record(s) to delete. Omitting the WHERE clause will result in all records in the table being deleted. |
Here is an excerpt from the “Customers” table in the Northwind sample database:
CustomerID |
CustomerName |
ContactName |
Address |
City |
PostalCode |
Country |
1 |
Alfreds Futterkiste |
Maria Anders |
Obere Str. 57 |
Berlin |
12209 |
Germany |
2 |
Ana Trujillo Emparedados y helados |
Ana Trujillo |
Avda. de la Constitución 2222 |
México D.F. |
05021 |
Mexico |
3 |
Antonio Moreno Taquería |
Antonio Moreno |
Mataderos 2312 |
México D.F. |
05023 |
Mexico |
4 |
Around the Horn |
Thomas Hardy |
120 Hanover Sq. |
London |
WA1 1DP |
UK |
5 |
Berglunds snabbköp |
Christina Berglund |
Berguvsvägen 8 |
Luleå |
S-958 22 |
Sweden |
The following SQL statement removes the customer “Alfreds Futterkiste” from the “Customers” table:
DELETE FROM Customers WHERE CustomerName=‘Alfreds Futterkiste’; |
Now, the “Customers” table will appear as follows:
CustomerID |
CustomerName |
ContactName |
Address |
City |
PostalCode |
Country |
2 |
Ana Trujillo Emparedados y helados |
Ana Trujillo |
Avda. de la Constitución 2222 |
México D.F. |
05021 |
Mexico |
3 |
Antonio Moreno Taquería |
Antonio Moreno |
Mataderos 2312 |
México D.F. |
05023 |
Mexico |
4 |
Around the Horn |
Thomas Hardy |
120 Hanover Sq. |
London |
WA1 1DP |
UK |
5 |
Berglunds snabbköp |
Christina Berglund |
Berguvsvägen 8 |
Luleå |
S-958 22 |
Sweden |
You can delete all rows in a table without removing the table itself. This ensures that the table structure, attributes, and indexes remain intact.
DELETE FROM table_name; |
The following SQL statement removes all rows from the “Customers” table without deleting the table itself:
DELETE FROM Customers; |