SQL with Examples : SQL DELETE Statement

Hits: 29

SQL DELETE Statement

The DELETE statement is used to delete data from a table. It can be used in two ways:

  1. To delete specific rows from a table
  2. To delete all rows from a table

Now let’s take a look at each of these in detail.

Syntax

DELETE FROM table_name WHERE condition;

Sample Table

To help you better understand the examples, and enable you to follow along with the tutorial, we are going to use the following sample table.

This table is part of an ‘Employee Management System’ that contains basic information about employees.

ID Name Age City Job Salary
1 Bob 28 New York Manager 60000
2 Eve 24 New York Developer 32000
3 Max 26 New York Janitor 9000
4 Kim 25 Chicago Manager 55000
5 Joe 23 Chicago Developer 30000
6 Sam 27 Chicago Janitor 10000

DELETE a Row

To delete a row from a table, you just specify which table to delete the row from, as well as specify which row to delete using the WHERE clause.

Let’s take a simple example. Suppose you wanted to delete the third employee (ID = 3) from the Employees table. The following query will delete it:

DELETE FROM Employees
WHERE ID = 3;

The contents of the ‘Employees’ table after the deletion are:

ID Name Age City Job Salary
1 Bob 28 New York Manager 60000
2 Eve 24 New York Developer 32000
4 Kim 25 Chicago Manager 55000
5 Joe 23 Chicago Developer 30000
6 Sam 27 Chicago Janitor 10000

The DELETE statement always starts with the name of the table from which you want to delete the row. In our case, it’s the ‘Employees’ table. The WHERE clause then tells the DBMS which row to delete. Here it’s the third row.

Note that when you delete a row, you remove the entire row. The DELETE statement does not remove specific columns from the row.

Special care must be taken when using DELELE, because if you accidentally omit the WHERE clause you will end up deleting every row in the table.

DELETE FROM Employees;

The contents of the ‘Employees’ table after the deletion are:

ID Name Age City Job Salary

DELETE Multiple Rows

It’s the WHERE clause that determines which rows to delete. To delete multiple rows, use a WHERE clause that selects only the rows you want to delete. For example, the following DELETE statement removes all ‘Developers’ from the Employees table:

DELETE FROM Employees
WHERE Job = 'Developer';

The contents of the ‘Employees’ table after the deletion are:

ID Name Age City Job Salary
1 Bob 28 New York Manager 60000
3 Max 26 New York Janitor 9000
4 Kim 25 Chicago Manager 55000
6 Sam 27 Chicago Janitor 10000

DELETE All Rows

As you know, omitting the WHERE clause deletes all rows from the table. But if you really want to remove all rows from the table, do not use DELETE. Instead, use the TRUNCATE TABLE statement that performs the same thing faster than DELETE (because data changes are not logged).

Let’s delete all rows from the Employees table using TRUNCATE TABLE statement.

TRUNCATE TABLE Employees;

The contents of the ‘Employees’ table after the deletion are:

ID Name Age City Job Salary

Remember! The DELETE statement deletes one or more rows from the table but never deletes the table itself. To delete a table definition as well as the table contents, issue the DROP statement.

DELETE Rows Referenced From Another Table

Sometimes you want to delete rows from one table that are referenced from another table. For example, you have a table called ‘Poor_Performance’, which contains the ID of employees whose performance is poor. The contents of the table ‘Poor_Performance’ are:

ID Name
1 Kim
3 Amy
4 Max

Now you want to delete an employee in the ‘Employees’ table if that employee appears in the ‘Poor_Performance’ table. To do this, you can use a subquery in your DELETE statement’s WHERE clause.

DELETE FROM Employees
WHERE ID IN (SELECT ID
             FROM Poor_Performance)

The contents of the ‘Employees’ table after the deletion are:

ID Name Age City Job Salary
2 Eve 24 New York Developer 32000
5 Joe 23 Chicago Developer 30000
6 Sam 27 Chicago Janitor 10000

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

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)

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.