SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL OPERATORS SQL COMMANDS SQL Queries

SQL Table

SQL TABLE SQL CREATE TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL UPDATE TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT INTO Statement

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join

SQL OPERATOR

SQL Comparison SQL LOGICAL Operator SQL Arithmetic

Difference

SQL vs NOSQL WHERE vs HAVING DELETE vs DROP GROUP BY vs ORDER BY DROP vs TRUNCATE SQL IN vs SQL EXISTS

MISC

SQL SubQuery SQL CASE Commit and Rollback in SQL Pattern Matching in SQL DDL Commands in SQL DML Commands in SQL Types of SQL Commands SQL COUNT SQL Primary Key SQL FOREIGN KEY SET Operators in SQL Check Constraint in SQL SQL EXCEPT SQL VIEW SQL WHERE Statement SQL CRUD Operation Where Condition in SQL TCL Commands in SQL Types of SQL JOINS SQL Nth Highest Salary SQL NOT OPERATOR SQL UNION ALL SQL INTERSECT SQL Data Definition Language SQL Data Manipulation Language SQL Data Control Language SQL CONSTRAINTS SQL KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL

How To

How to use the BETWEEN operator in SQL How To Use INNER JOIN In SQL How to use LIKE in SQL How to use HAVING Clause in SQL How to use GROUP BY Clause in SQL How To Remove Duplicates In SQL How To Delete A Row In SQL How to add column in table in SQL ? How to drop a column in SQL? How to create a database in SQL? How to use COUNT in SQL? How to Create Temporary Table in SQL? How to Add Foreign Key in SQL? How to Add Comments in SQL? How To Use Group By Clause In SQL How To Use Having Clause In SQL How To Delete Column In Table How To Compare Date In SQL

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



ADVERTISEMENT
ADVERTISEMENT