×

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

SQL CONSTRAINTS

SQL Constraints specifies the rules/limitations/restrictions for data present in table. SQL Constraints are specified at the time of table creation or after table creation using ALTER command. There are two...

5 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 Left Join

The SQL Left Join query displays all the records from the table and displays similar records from the right table. The query displays zero records if it doesn’t find any...

4 minutes read.

SQL Tutorial for Beginners

SQL tutorial provides basic and advanced concepts of Structured Query Language and how you deploy SQL to work with a relational database system. Our SQL tutorial is designed for beginners...

3 minutes read.

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

DDL Commands in SQL

DDL stands for Data Definition Language. Data Definition Language is a bunch of SQL commands used to create, modify and delete Database schema but not the records or data. Data Definition...

5 minutes read.

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

How to remove duplicates in SQL

Introduction There are some specific rules that needs to be followed while creating the database objects. To improve the performance of a database, a primary key, clustered and non-clustered indexes, and...

9 minutes read.

How to use the BETWEEN operator in SQL

In this entire SQL article, we will understand and learn about the BETWEEN operator concept and how to use it in SQL. What is the BETWEEN operator in SQL? The Between operator...

4 minutes read.

SQL Data Types

In SQL DATA TYPES, each column holds value. Data types can be an integer type, character type, etc., and such data is stored in the database table. The data type is...

4 minutes read.

SQL GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is...

4 minutes read.

How to Create Temporary Table in SQL?

How to Create Temporary Table in SQL  Introduction to Temporary Tables Temporary table is a table which is used to store temporary data that can be used further in the same client...

3 minutes read.

SQL Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table. A PRIMARY KEY...

4 minutes read.

WEB SQL

What is Web SQL? In SQL we can create databases, read the data in the databases, insert the records into the databases, and delete the records from the databases. For storing...

4 minutes read.

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 minutes read.

Check Constraint in SQL

The Check Constraint in SQL is the rule or set of rules used to limit the data range that can be entered in a table column. Check constraint is used...

8 minutes read.

DELETE VS DROP in SQL

This page contains all the information about the Delete command and Drop command concept and the difference between the DELETE and DROP commands in SQL. What is the DELETE command in...

3 minutes read.