Introduction to SQL DELETE
SQL DELETE statement is a data manipulation language (DML) command that is used to remove one or more rows or data records from a database table or view in SQL database servers. DELETE command can be used within the body of a user-defined function. Since it does not delete the rows permanently, a DELETE transaction can be rolled back.
DELETE statement can be used along with conditional and limiting clauses such as TOP and WHERE for filtering and removing only specific rows. Albeit, DELETE might sound fancier by now as it has higher interoperability, provisions for filtering rows for deletion and rollback, but its performance gets compromised when removing all the rows from the database table. Ergo, in such situations, we should use the TRUNCATE command because it’s faster than DELETE.
There are a few other commands in SQL like DROP and TRUNCATE table which is also used to remove data from the database objects. But they differ from each other slightly in the following ways :
Differences between DELETE, TRUNCATE, and DROP TABLE statements
|DELETE TABLE||TRUNCATE TABLE||DROP TABLE|
|DELETE statement removes one or multiple rows from the database objects.||TRUNCATE statement that removes all the data rows from the database object.||DROP statement that removes all the records in the table and also destroys the table structure.|
|It is a Data Manipulation Language(DML) statement.||It is a Data Definition Language(DDL) statement.||It is a Data Definition Language(DDL) statement.|
|We can use a WHERE or TOP clause.||We cannot use a WHERE or TOP clause.||We cannot use a WHERE or TOP clause.|
|We can perform a rollback operation to undo a DELETE table statement.||We cannot perform a rollback operation to undo a TRUNCATE table statement.||We cannot perform a rollback operation to undo a DROP table statement.|
|Table rows are not deleted permanently.||Table rows are deleted permanently.||Table and all its relationships, index, privileges, etc are deleted permanently.|
The basic syntax for writing the DELETE statement in SQL is as follows :
DELETE FROM table_name;
- table_name : Name of the table or view that has to be removed.
The good thing about the DELETE statement is that it allows us to limit the rows which have to be removed from the table. Follow the following syntax for the same.
DELETE [TOP (expression | percentage)]
The arguments used in this piece of syntax is as follows :
- expression | percentage: Expression stands for the number of top rows that have to be deleted. If we don’t have any specific number in mind, we can use the percentage instead to limit the number of rows for deletion.
- condition: It is the filtering condition for selecting only a specific number of rows for removal.
Having discussed the syntax and arguments used in the syntax, we are all set to try a few examples based on DELETE statement in SQL.
Examples of SQL DELETE
In order to illustrate the basic functionality of SQL DELETE statements, what could be better than trying a few examples on data records of a dummy table. Ergo, let us first create a table called “customer_june”. We can use the following code snippet to perform the said task.
CREATE TABLE customer_june
customer_name character varying(255),
city character varying(255),
contact_no character varying(255),
email_address character varying(255)
After successfully creating the table, let us first insert a few data records in the table to work with. Use the following insert statement for the same. For the uninitiated, INSERT statement is used for inserting new data rows in a database table.
INSERT INTO customer_june(
customer_id, customer_name, city, contact_no, email_address)
VALUES (1001,'Rahul Sharma','Mumbai','9878987823','firstname.lastname@example.org'),
(1002,'George Cooper','Kanhas City','9090909089','NULL'),
(1005,'Gloria Gonsalez','New York','7545754535','email@example.com'),
(1006,'Rahul Kashyap','New Delhi','9876504321','firstname.lastname@example.org');
select * from customer_june;
Now we are all set to try a few examples to illustrate the basic functionality of DELETE statements.
Example #1: Basic DELETE query to delete or remove records from a database object such as a database table or view.
DELETE FROM customer_details;
The query has returned successfully. This statement will remove all the rows from the customer_details table while keeping the table structure intact.
Example #2: DELETE statements with WHERE to remove only specific rows from the database table.
DELETE FROM customer_june
WHERE city = 'Kanhas City';
Now we have successfully deleted the row with misspelt city name from the “customer_june” table. The final data looks something like this after modifications.
SELECT * FROM customer_june;
Example #3: DELETE statements with subqueries to remove only specific rows from the database table.
DELETE FROM customer_june
WHERE EXISTS (SELECT * FROM customer_june
WHERE city LIKE '%New%'
ORDER BY customer_id DESC
LIMIT 1) ;
The query has returned successfully. The purpose of this query was to illustrate usage of subqueries in DELETE statements. We can try a number of subquery permutations and combinations in the conditional expression part when filtering records.
DELETE statement in SQL is used to remove one or more rows from a database table or view. It is a DML command. We can use conditional clauses such as WHERE and TOP within DELETE query to filter out and remove only specific rows.
This is a guide to SQL DELETE. Here we discuss the introduction, differences, Syntax, and Parameters with examples respectively. You may also have a look at the following articles to learn more –