Beginners Guide to SQL – SQL DELETE Statement

Hits: 17

(SQL Tutorials for Citizen Data Scientist)

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.

Example

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:

Example

DELETE FROM persons;

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

 

Beginners Guide to SQL – SQL DELETE Statement

 

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!