SQL DELETE Query
Video Tutorial
Overview
When we work with a database table, we might come across records that are no longer required and hence, need to be deleted. We use the DELETE query in SQL to remove unwanted records from a given table. We can make use of the WHERE clause to delete only specific rows which satisfy a given condition.
Scope of Article
- We will cover the DELETE query in SQL to remove all records from a table.
- We will use the WHERE clause to delete records based on a filter condition.
- We will use logical operators in filter conditions to delete multiple records.
Introduction to DELETE Query in SQL
Have you ever wondered what happens when you delete your account on a social media platform or hit unsubscribe from a mailing list? It may seem like a simple action, but have you ever considered what happens in the backend that actually removes your information from their records?
When you click the Delete account or Unsubscribe button, the backend communicates with the database and deletes the row with your information. Once your data is removed, you will no longer be able to log in to the social media platform or receive any emails from the concerned mailing list since you do not exist as a user in their directory.
In the database layer, we run the DELETE query for the user whose data is no longer required and the account needs to be deleted. This is where SQL comes in - SQL (Structured Query Language) is a powerful programming language used to manage relational databases. One of its key features is the ability to delete records from a table using a DELETE query.
In the further sections of this article, we will dive deeper into how to write a DELETE query in SQL to remove unwanted records from our table. But before that, let's set up a sample employee table to use as an example. By the end of this article, you'll have a clear understanding of how to use DELETE queries to manage your database and keep your data organized.
Syntax
The syntax for a DELETE query in SQL is as follows:
- table_name: denotes the name of the table from which the records are required to be deleted.
- condition: specifies the criteria that must be met for a record to be deleted.
Example
Sample Table
Suppose we have a table employee with details of all employees working at a company 'X'. The table has not been updated for a long time and we still have the data of all employees, even if they left the organization years back. Now we don't really need these extra records, right? To remove these extra records, we use the DELETE query in SQL.
Let us first create an employee table that has the Employee ID, First Name, Last Name, Work Location, and Designation of each employee using the below query.
We then enter some records into the empty employee table.
Once the above data is inserted, we run the below SELECT query to display all the records in our employee table.
Output:
EmployeeID | FirstName | LastName | WorkLocation | Designation |
---|---|---|---|---|
62549 | John | Doe | Bangalore | Software Engineer |
82743 | Priya | Sharma | Hyderabad | DB Administrator |
58461 | Raj | Kumar | Gurugram | Technical Architect |
95462 | Ravi | Bangalore | Manager | |
25947 | Shreya | P | Chennai | Software Engineer |
42650 | Jane | Scott | Hyderabad | Analyst |
Delete Records Based on Filter Condition in SQL
Suppose in the above table, we need to delete the records of an employee with ID 58461, who has left the organization. We can use the WHERE clause to specify a condition that filters all employees having ID 58461. All the filtered records can now be deleted, leaving the other records intact. To delete the records in our employee table where the EmployeeID is 58461, the SQL query will look like this:
Output:
We get the following output when we run the SELECT query again on the employee table.
EmployeeID | FirstName | LastName | WorkLocation | Designation |
---|---|---|---|---|
62549 | John | Doe | Bangalore | Software Engineer |
82743 | Priya | Sharma | Hyderabad | DB Administrator |
95462 | Ravi | Bangalore | Manager | |
25947 | Shreya | P | Chennai | Software Engineer |
42650 | Jane | Scott | Hyderabad | Analyst |
Notice that the record of the employee having ID as 58461 is deleted from the employee table.
Explanation:
The DELETE query first filters the records based on the condition if EmployeeID is equal to 58461. For all the records where this condition is True, it will execute the DELETE command and remove the rows from the table. We can also use other arithmetic operators like <, >, <=, >=, etc in our filter condition.
Using Logical Operators in the Filter Condition
Suppose we want to have more than one filter condition in our WHERE clause. For example, we want to delete records of employees who are both - Software Engineers and are from Bangalore. Since the filter condition is a boolean expression, we can make use of logical operators like AND and OR to specify more than one condition.
The syntax would remain the same as earlier, i.e. we will still use the WHERE clause. Also, we will use the same parameters: tableName and filterConditions. There can be multiple filter conditions, separated by AND or OR operators, in the form of boolean expressions.
- Example (AND Operator):
Let us delete the records of all Software Engineers from Bangalore. The SQL query will have two conditions - one for checking if the employee is a Software Engineer and another for checking if he is from Bangalore, separated by the AND clause.
Note: AND operator is used to signify that all the conditions must be true to delete a given record.
Output:
We get the following output when we run the SELECT query on the employee table.
EmployeeID | FirstName | LastName | WorkLocation | Designation |
---|---|---|---|---|
82743 | Priya | Sharma | Hyderabad | DB Administrator |
95462 | Ravi | Bangalore | Manager | |
25947 | Shreya | P | Chennai | Software Engineer |
42650 | Jane | Scott | Hyderabad | Analyst |
Explanation:
The DELETE query filtered all the records where both the WorkLocation was Bangalore and the Designation was Software Engineer. It then deleted all the rows which matched the above criteria.
- Example (OR Operator):
Note that the above query does not delete the rows where only one condition is satisfied. If we want to delete the records of employees who are either Software Engineers or are from Bangalore, we will use the OR clause. The SQL query will be:
Note: OR operator means that either one of the conditions should be true to delete a given record.
Output:
We get the following output when we run the SELECT query on the employee table.
EmployeeID | FirstName | LastName | WorkLocation | Designation |
---|---|---|---|---|
82743 | Priya | Sharma | Hyderabad | DB Administrator |
42650 | Jane | Scott | Hyderabad | Analyst |
Explanation:
The DELETE query filtered all the records where either the WorkLocation was Bangalore or the employee's Designation was Software Engineer. It then deleted all the rows which matched either of the above criteria.
Delete All Records in SQL
We have 2 records left in our employee table after all the delete operations. What if we want to empty the entire table instead? Well, you would usually not want to do this unless the bugs haven't kept you awake enough!!
Proceed at your own risk! We will write the same DELETE query as above but this time, we will not specify any filter condition.
Syntax:
DELETE FROM *tableName*
Parameters:
We need only one parameter: tableName which has the name of the table to be emptied.
Example:
Let us now delete all the remaining records from the employee table. The SQL query will be:
Note: The above query will delete all the records in the table without changing its structure. This means that the employee table will still exist but with 0 records.
Output:
No Result: Whoops! We have deleted all the records from the employee table, hence, we do not get any output on running the SELECT query.
Explanation:
Since we have not specified any filter condition using the WHERE clause, the DELETE query removed every row from the employee table. This resulted in an empty table with no records visible.
Note: To remove all the records from an SQL table, we can also use TRUNCATE. It is a DDL command which resets the table structure and is usually faster than DELETE but we cannot use filter conditions to specify which records can be deleted.
Conclusion
- We use the DELETE query in SQL to remove unwanted records from a table.
- The DELETE query can take two parameters: tableName and filterConditions.
- We can use the WHERE clause to delete only those records which satisfy a given boolean condition.
- We can also use logical operators like AND and OR in the filter condition. It may result in the deletion of more than one record from the table.
- If we do not specify a filter condition, the DELETE query will delete all the records from the given table.
- While TRUNCATE too can be used to delete all records from a table, it will also reset the table structure and is comparatively faster than DELETE.