Curriculum
Course: SQL
Login

Curriculum

SQL

SQL References

0/80

MySQL Functions

0/139

SQL Server Functions

0/84

SQL Quick Ref

0/1
Text lesson

SQL DELETE

The SQL DELETE Statement

The DELETE statement serves to remove pre-existing records from a table.

DELETE Syntax

DELETE FROM table_name WHERE condition;

 

Important: Exercise caution when deleting records from a table! Pay attention to the WHERE clause in the DELETE statement. The WHERE clause determines which record(s) to delete. If you omit the WHERE clause, all records in the table will be deleted!

Demo Database

The following excerpt is taken from the Customers table utilized in the illustrations:

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 Example

This SQL statement removes the customer “Alfreds Futterkiste” from the “Customers” table:

Example

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

Delete All Records

You can delete all rows in a table while preserving its structure, attributes, and indexes, thereby ensuring that the table remains intact.

DELETE FROM table_name;

This SQL statement clears all rows from the “Customers” table, while leaving the table itself intact:

Example

DELETE FROM Customers;

Delete a Table

To entirely remove the table, utilize the DROP TABLE statement:

Example

Delete the Customers table.

DROP TABLE Customers;