SQL DELETE Statement
The DELETE
statement in SQL removes rows from a table. It's a powerful command, but use it carefully because deleting data is usually irreversible unless you have a backup of your database.
DELETE: Definition and Usage
DELETE
removes rows based on conditions specified in the WHERE
clause. The WHERE
clause is absolutely essential; omitting it will delete *every single row* in the table, which is rarely the intention and can lead to significant data loss.
Syntax
Syntax
DELETE FROM table_name
WHERE condition;
Example Database
Here is a sample 'Customers' table used in the examples:
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 |
Examples
Deleting a Specific Row
This deletes the row where 'CustomerName' is 'Alfreds Futterkiste'.
Syntax
DELETE FROM Customers
WHERE CustomerName = 'Alfreds Futterkiste';
Output
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 |
Deleting All Rows (Caution!)
This deletes all rows from the 'Customers' table. The table structure remains; only the data is removed. Use this with extreme caution!
Syntax
DELETE FROM Customers;
Output
The 'Customers' table will be emptied of all data; the table structure remains.
Completely Deleting a Table (DROP TABLE)
To completely remove a table and its structure, use DROP TABLE
. This action is irreversible (without a backup).
Syntax
DROP TABLE table_name;
Output
(The table and its data are permanently deleted.)