×

SQL DELETE

In this tutorial, you will learn about the SQL DELETE concept by using examples.

In Structured Query Language (SQL), we fetch the data from the database table using SELECT Statement and modify the data using the UPDATE Database. What if we want to remove or delete the data from the table?  

If you need to remove or delete the row or table from the database, you have to use the DELETE Statement. Also, you can delete or remove one row or multiple rows from the table using a condition.

The syntax for the SQL DELETE query is as follows:

DELETE FROM TABLE_NAME;

The above syntax will delete all the records or data from the table specified in the delete query.

What if we want to delete a single row from a table? Then in such a case, we will use the Where Clause, which is optional in the delete statement, where the clause is used to identify the deleted row.

Syntax:

DELETE FROM TABLENAME WHERE [CONDITIONS];

Let's understand the concept of the SQL DELETE command with the help of an example.

Consider the already existing table with the following data:

Table Name: Employee_Details

E_IdE_NameE_SalaryE_CityDesignationAge
1001Kirti Kirtane60000MumbaiProject Manager26
1002Akash Vani40000PuneSystem Engineer22
1003Anupam Mishra55000HyderabadProject Manager25
1004Anuj Rawat45000HyderabadSoftware Tester25
1005Akanksha Yadav42000PuneAssociate Software Developer23
1006Bhavesh Wani50000MumbaiAssociate Software Developer24
2001Sakshi Sharma40000BangaloreSystem Engineer23
2002Megha Ghatole45000BangaloreSoftware Tester24
2003Surbhi Nahar60000PuneProject Manager26
2004Supriya Shende55000MumbaiSoftware Developer25
2005Prachi Sharma52000HyderabadSoftware Developer24
2006Purva Dhandekar50000BangaloreSoftware Tester23
3001Shruti Deshpande60000PuneProject Manager26
3002Rohit Nikam40000HyderabadSystem Engineer23
3003Sahil Jain50000MumbaiSoftware Developer24

Example1: You want to delete the data by employee id from table employee_details. We will execute the below query using the where clause as follows:

DELETE FROM Employee_Details WHERE E_Id = 3003;

The above statement will delete the employee's data whose id number is 3003 from the employee's table.

To check whether the above query is successfully executed or not, we will execute the SELECT statement.

The output of the above query is as follows:

SELECT * FROM Employee_Details;
E_IdE_NameE_SalaryE_CityDesignationAge
1001Kirti Kirtane60000MumbaiProject Manager26
1002Akash Vani40000PuneSystem Engineer22
1003Anupam Mishra55000HyderabadProject Manager25
1004Anuj Rawat45000HyderabadSoftware Tester25
1005Akanksha Yadav42000PuneAssociate Software Developer23
1006Bhavesh Wani50000MumbaiAssociate Software Developer24
2001Sakshi Sharma40000BangaloreSystem Engineer23
2002Megha Ghatole45000BangaloreSoftware Tester24
2003Surbhi Nahar60000PuneProject Manager26
2004Supriya Shende55000MumbaiSoftware Developer25
2005Prachi Sharma52000HyderabadSoftware Developer24
2006Purva Dhandekar50000BangaloreSoftware Tester23
3001Shruti Deshpande60000PuneProject Manager26
3002Rohit Nikam40000HyderabadSystem Engineer23
SQL DELETE

There is no such data for employee id 3003 in the table because we removed the data from the table.

The above query deletes one row from the table. What if you want to delete multiple rows at a time from the table? Then try the below example or statement.

Example2: Now, if you want to delete data of those employees who belong to the city name 'Hyderabad', then we will execute the below delete statement:

DELETE FROM Employee_Details WHERE E_City = ‘Hyderabad’;

The Above query will delete all the data of employees from the employee_details table who belong to the city name Hyderabad.

To check whether the above query is successfully executed or not, we will execute the SELECT statement.

The output of the above query is as follows:

SELECT * FROM Employee_Details;
E_IdE_NameE_SalaryE_CityDesignationAge
1001Kirti Kirtane60000MumbaiProject Manager26
1002Akash Vani40000PuneSystem Engineer22
1005Akanksha Yadav42000PuneAssociate Software Developer23
1006Bhavesh Wani50000MumbaiAssociate Software Developer24
2001Sakshi Sharma40000BangaloreSystem Engineer23
2002Megha Ghatole45000BangaloreSoftware Tester24
2003Surbhi Nahar60000PuneProject Manager26
2004Supriya Shende55000MumbaiSoftware Developer25
2006Purva Dhandekar50000BangaloreSoftware Tester23
3001Shruti Deshpande60000PuneProject Manager26
SQL DELETE

Employee ID numbers 1003, 1004, 2005, and 3002 are deleted from the table because that employee id belongs to the city Hyderabad which we deleted.

We learn single row deletion from the table and multiple row deletion. What if you want to delete or remove all the records from the table? For such a scenario, try the below example query or statement.

Example3: Suppose you want to delete all the records from the employee_details table. Use the below query as follows:

DELETE FROM Employee_Details;

The above query will delete all the data from the table.

To check whether the above query is successfully executed or not, we will execute the SELECT statement.

The output of the above query is as follows:

SELECT * FROM Employee_Details;

Output

SQL DELETE

Related Topics

Pattern Matching in SQL

The LIKE in the Structured Query Language is a Logical Operator. The SQL LIKE is used within the WHERE clause in the SQL. This Like Operator is used with the...

5 minutes read.

SQL Syntax

In this tutorial, we will help you understand about the different SQL Syntax concepts with the help of an example. Every Structured Query Language starts with Keywords like select, insert, update,...

5 minutes read.

SQL WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

9 minutes read.

SQL DELETE

In this tutorial, you will learn about the SQL DELETE concept by using examples. In Structured Query Language (SQL), we fetch the data from the database table using SELECT Statement and...

3 minutes read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 minutes read.

Where Condition in SQL

WHERE clause is used with the Data Manipulation Language Command in SQL, the Data Manipulation Language commands are the SELECT statement, UPDATE statement, and DELETE statement. WHERE clause condition is an...

5 minutes read.

SQL Aggregate Functions

In this tutorial, we will learn and understand the SQL Aggregate Functions concept with the help of examples. SQL Aggregate Function is a function used to perform calculation operations on one...

4 minutes read.

SQL DROP Database

This tutorial will help us understand how to drop a database from the SQL Server with a few examples. The DROP command removes all the objects stored in the database and...

4 minutes read.

SQL SELECT SUM

The SQL Sum() function is an aggregate function in SQL that returns the total values of an expression. The expression may be numerical, or it may be an expression. Syntax: SELECT SUM(columnname)...

3 minutes read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

Drop vs Truncate in SQL

In this article, we will learn and understand the Drop and Truncate commands and the difference between these two commands. What is Drop Command? Drop is a Data Definition Language command in...

6 minutes read.

Commit and Rollback in SQL

In Structured Query Language, we have Data Definition Language (DDL) and Data Manipulation Language (DML) commands the same way we have Transaction Control Language (TCL) commands in the Structured Query...

4 minutes read.

How to add column in table in SQL ?

How to add column in table in SQL  Introduction To add a column in already created table, one needs to use the ALTER command along with the ADD clause.If in the query,...

7 minutes read.

SQL SELECT AVG

In this tutorial, we will learn about the aggregate function name avg() function concept in SQL with the help of examples. The AVG() function is one of the aggregate functions in...

3 minutes read.

SQL UNION ALL Operator

This page has all the information about UNION ALL operator, which is used to join all the values from the SELECT queries. Similar records were not considered in the result in...

3 minutes read.

SQL Aggregate Operators

In SQL, the aggregate operators perform a calculation on multiple rows or multiple values of a single column and give the output a single value. Here, multiple rows of data are...

4 minutes read.

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

6 minutes read.

space() function in SQL

 This function returns a string with a specified number of spaces. If we specify a number, it returns the strings having that specified number of spaces. SPACE Function is available...

2 minutes read.

Difference between SQL and NoSQL

SQL vs. NoSQL | Difference between SQL and NoSQL Choosing a database is the most fundamental decision that needs to be decided before starting a task. Relational and non-relational databases are...

3 minutes read.

SQL Formatter

As a beginner, one does not focus much on Formatting the queries while writing their code. This leads to a great confusion while referring the code in future. For a...

5 minutes read.