SQL BASIC
SQL JOINS
SQL ADVANCED
SQL REFERENCE

SQL DELETE Statement

In this tutorial you will learn how to delete records from a database table using SQL.

Deleting Data from Tables

Just as you insert records into a table with the INSERT statement, you can delete records from a table as well with the DELETE statement.

Syntax

The DELETE statement is used to remove one or more rows from a table.

DELETE FROM table_name WHERE condition;

Warning: The WHERE clause in the DELETE statement specifies which record or records should be deleted. It is however optional, but if you omit or forget the WHERE clause, all the records will be deleted permanently from the table.

Let's delete some records from the persons table that we've created in create table chapter.

Suppose that our persons table currently has the following records:

+----+--------------------+------------+-------------+
| id | name               | birth_date | phone       |
+----+--------------------+------------+-------------+
|  1 | Peter Wilson       | 1990-07-15 | 0711-020361 |
|  2 | Carrie Simpson     | 1995-05-01 | 0251-031259 |
|  3 | Victoria Ashworth  | 1996-10-17 | 0695-346721 |
|  4 | George Bailey      | 1993-03-05 | 0897-034214 |
|  5 | Norman Bates       | 1999-08-25 | 0522-556721 |
+----+--------------------+------------+-------------+

Delete Records Based on Conditions

The following statement will delete the rows from persons table where id is greater than 3.

DELETE FROM persons WHERE id > 3;

After executing the query, the persons table will look something like this:

+----+--------------------+------------+-------------+
| id | name               | birth_date | phone       |
+----+--------------------+------------+-------------+
|  1 | Peter Wilson       | 1990-07-15 | 0711-020361 |
|  2 | Carrie Simpson     | 1995-05-01 | 0251-031259 |
|  3 | Victoria Ashworth  | 1996-10-17 | 0695-346721 |
+----+--------------------+------------+-------------+

Note: See the tutorial on SQL WHERE clause to know how to form complex query based on multiple conditions while deleting the records from the tables.


Delete All Data

Similarly, as mentioned above if you do not specify the WHERE clause in the DELETE statement all the rows from the table will be deleted. However, the target table itself won't be deleted that means the table structure, attributes, and indexes will remain intact.

The following statement will remove all the records from the persons table:

DELETE FROM persons;

Now, if you try to select the records from the persons table you'll get an empty result set.

Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties