(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 FROMtable_name
WHEREcondition
;
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
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
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.