May 16, 2021 SQL
The DELETE statement is used to delete existing records in the table.
DELETE FROM table_name
WHERE condition;
Please note
Be careful when deleting records in a table! Note the WHERE clause in the SQL DELETE statement! The WHERE clause specifies which records need to be deleted. If the WHERE clause is omitted, all records in the table will be deleted! |
In this tutorial, we'll use the famous Northwind sample database.
Here's the data from the Customers table:
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 |
Suppose we want to remove customer "Alfreds Futterkiste" from the "Customers" table.
Let's use the following SQL statement:
Now, the Customers table looks like this:
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 deleting the table. This means that the structure, properties, and index of the table remain the same:
DELETE FROM table_name;
Or
DELETE * FROM table_name;
Note: Take extra care to delete records without backups! Because you deleted can not repeat!
Now, take a look at your mastery of sql Delete statements (delete records in tables) with the following topics!
SQL Delete Statement: In this section of the quiz, you'll practice using SQL Delete statements by choosing to fill in the blanks.
Note: The above tests are paid and premium VIP is free of charge
For more questions, please refer to: SQL Quiz Library