How to Delete Records Using Delete in SQL [Updated] (2024)

Data management can be a challenge given the massive amounts of data to deal with. Anyone who manages data knows that sometimes, we need to delete some data from our database, due to changes in the real-world. For example, when a store stops selling an item, its record is removed from the inventory database. This is done with the help of the SQL Delete command. This command is a significant and integral part of this query language.

What Is Delete in SQL?

The Delete command in SQL is a part of the Data Manipulation Language, a sub-language of SQL that allows modification of data in databases. This command is used to delete existing records from a table. Using this, you can either delete specific records based on a condition or all the records from a table.

NOTE: It is crucial to be entirely sure about using this command, as it deletes the data permanently.

To start using the Delete command, we need to know its syntax.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program

How to Delete Records Using Delete in SQL [Updated] (1)

The Syntax for Using the SQL Delete Command

DELETE FROM table_name

WHERE [condition];

  • The table from which we want to delete rows is specified in the table_name parameter of the DELETE FROM statement.
  • There is an optional WHERE clause in which we can specify the condition according to which the rows should get deleted.

NOTE: Extra caution needs to be taken with the WHERE clause as, without it, all the rows from the table will get deleted.

Multiple conditions can be specified in the WHERE clause with the help of AND and OR operators.

Let’s apply the concepts stated above to the rows of a table.

Deleting Specific Rows Based on a Single Condition

Let’s take a sample “Employee_details” table;

How to Delete Records Using Delete in SQL [Updated] (2)

  • From the table depicted above, to delete the employee’s record with “EmployeeID” equal to one, we’ll use the following query:

How to Delete Records Using Delete in SQL [Updated] (3)

  • To check the result of the query in the table above, we’ll use the ‘SELECT *’ command:

How to Delete Records Using Delete in SQL [Updated] (4)

As we can see, one record has been deleted based on the condition specified in the WHERE clause.

  • From the table above, we need to delete the record of the employee with the name “Arun”:

How to Delete Records Using Delete in SQL [Updated] (5)

As we can see, “Name” is of character string data type, so the value being specified has to be enclosed in single inverted commas, without which the system will return a syntax error.

The query above will result in the following:

How to Delete Records Using Delete in SQL [Updated] (6)

  • We can also delete multiple rows using a single query.

From the table displayed above, we need to delete all the employee records with salary less than 60000:

How to Delete Records Using Delete in SQL [Updated] (7)

This will result in the following:

How to Delete Records Using Delete in SQL [Updated] (8)

As we can see, four rows have been deleted.

More than one condition can be applied to the columns using a single query. Let’s see how that is done.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program

How to Delete Records Using Delete in SQL [Updated] (9)

Deleting Specific Rows Based on Multiple Conditions

We can use AND and OR operators to combine multiple conditions in the WHERE clause of a DELETE command.

  • When the OR operator is used, all the rows that satisfy either one of the specified conditions, are deleted.

From our sample “Employee_details” table, to delete all employee records where the employee name is “Ajay” or the city is “Chennai”:

How to Delete Records Using Delete in SQL [Updated] (10)

This will result in the following:

How to Delete Records Using Delete in SQL [Updated] (11)

As we can see, both the rows satisfying either of the conditions have been deleted.

  • When we use the AND operator, only the rows satisfying both the conditions being specified are deleted.

From the above table, we need to delete all the records for employees that belong to “Bangalore” and have a salary less than 50000:

How to Delete Records Using Delete in SQL [Updated] (12)

This will result in the following:

How to Delete Records Using Delete in SQL [Updated] (13)

As we can see, only the one record that satisfies both the conditions has been deleted.

Let’s see what happens when we don’t use the WHERE clause.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program

How to Delete Records Using Delete in SQL [Updated] (14)

Deleting All the Records From a Table

It’s imperative to be extremely careful while using the DELETE command on a table as the data gets deleted permanently.

  • Without a WHERE clause, all the records of the table are deleted. If not done intentionally, this could cause a lot of problems.

To delete all the records from our “Employee_details” table, we’ll use the following query:

How to Delete Records Using Delete in SQL [Updated] (15)

To check whether all the records are deleted, we’ll use the SELECT command:

How to Delete Records Using Delete in SQL [Updated] (16)

As we can see, the SELECT command returns an empty set as all the records from the “Employee_details” table have been deleted.

  • We can also use the TRUNCATE command to delete all the records from a table.

This is a part of the Data Definition Language, a sub-language of SQL that allows the creation and modification of database objects. The syntax of this command is:

TRUNCATE table_name;

As we can see, there is no ‘WHERE’ clause, so this command is used only when we need to empty the contents of a table.

To delete all the records from the “Employee_details” table:

How to Delete Records Using Delete in SQL [Updated] (17)

To check whether all the records have been deleted:

How to Delete Records Using Delete in SQL [Updated] (18)

As we can see, the records have been deleted and the table returns an empty set.

Next Steps

Deleting records from tables and databases is a huge part of managing data. Sometimes, we insert the wrong data, or the data in a table becomes outdated and needs modification, all of this requires the help of Delete in SQL.

Check out our tutorials to learn how to delete duplicate rows in tables and many SQL statements.

Now that you know how to delete existing records, it is time for you to start learning about other SQL commands so that you can start manipulating and querying data and move forward in your journey to become an expert in SQL. If you liked this article, you must check out our Post Graduate Program in Full Stack Web Developmentas it covers the A-Z of SQL as well.

Do you have any questions for us? Mention them in the comment section of our “Delete in SQL” article, and we’ll have our experts in the field answer them for you.

How to Delete Records Using Delete in SQL [Updated] (2024)
Top Articles
Latest Posts
Article information

Author: Sen. Ignacio Ratke

Last Updated:

Views: 5801

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Sen. Ignacio Ratke

Birthday: 1999-05-27

Address: Apt. 171 8116 Bailey Via, Roberthaven, GA 58289

Phone: +2585395768220

Job: Lead Liaison

Hobby: Lockpicking, LARPing, Lego building, Lapidary, Macrame, Book restoration, Bodybuilding

Introduction: My name is Sen. Ignacio Ratke, I am a adventurous, zealous, outstanding, agreeable, precious, excited, gifted person who loves writing and wants to share my knowledge and understanding with you.