Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

SQL Delete statement (delete records in table)


May 16, 2021 SQL


Table of contents


SQL DELETE statement


The DELETE statement is used to delete existing records in the table.

SQL DELETE syntax

DELETE FROM table_name
WHERE condition;

SQL Delete statement (delete records in table) 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!

Demonstrate the database


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

SQL DELETE instance


Suppose we want to remove customer "Alfreds Futterkiste" from the "Customers" table.

Let's use the following SQL statement:

DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste';

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

Delete all data


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!


Chapter test


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.

Click here to test

Note: The above tests are paid and premium VIP is free of charge

For more questions, please refer to: SQL Quiz Library