Introduction to SQL DELETE Statement
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, and provisions for filtering rows for deletion and rollback, 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 are 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 Statement
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','email@example.com'),
(1002,'George Cooper','Kanhas City','9090909089','NULL'),
(1005,'Gloria Gonsalez','New York','7545754535','firstname.lastname@example.org'),
(1006,'Rahul Kashyap','New Delhi','9876504321','email@example.com');
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.
SQL DELETE Statement
- Basically, this statement is used to delete the existing records from the table. We need to define the table name from which we have deleted the records.
- In the below first syntax we have not used the where condition so all the records from a table will be deleted. In the fourth syntax, we have used only on where condition so all the rows will be deleted that match this condition. In the second syntax, we are using two conditions with a where clause so data is deleted at the time matching both the condition.
- Delete from name_of_table;
- Delete from name_of_table where name_of_column1 = value AND name_of_column = values;
- Delete from name_of_table where name_of_column <comparison operator> values;
- Delete from name_of_table where condition;
Below is the parameter description of syntax as follows.
- Delete – This statement is used to delete the rows from a table. We can use delete statement with and without where condition to delete rows from the table.
- From – This is the SQL keyword that was used to define the table name from which we have deleted the records.
- Name of table – This is nothing but the table name from which we have deleted the records. At the time of deleting records or executing a query, we need to define the table name.
- Where condition – This is the most important parameter at the time of execution. The where condition in the delete statement of SQL will be used to delete the specified row which was we have defined in the where condition. Where condition is very useful to delete specific condition row by using the sql delete statement.
- Name of column – This is the table column that was used to delete data from a table. If suppose we have used delete statement with specified column it will deleting the records from the table.
- AND – This keyword is used in the delete statement to delete the rows from the specified column that we have defined in the query.
- At the time of using a single column in where condition by using delete query it will be deleting the specified row which was matches the condition.
SQL DELETE Query
- SQL delete query is not case sensitive we can use DELETE or delete to execute the sql delete query.
- The below example shows that the query is not case sensitive. In the below first example we have used sql delete query in the lowercase letter, after using sql delete query it will execute without issuing any error. In the second example, we have used sql delete query in uppercase letter, after using a delete query in the uppercase letter it will also execute without issuing any error. So we can say that the SQL delete query is not case sensitive.
delete from sql_insert where id = 1001; DELETE from sql_insert where id = 1001;
- At the time of executing any delete query on the production database server, we need to be very careful, because it may take downtime of the system.
- Suppose our application needs to contain only new records and we have to need to delete old records same time we can use this statement to delete old records from the table.
- To delete the records from a table we can also use the comparison operator, after using the comparison operator the matched rows will be deleted.
- Below is the example of SQL delete statements are as follows. We are using sql_delete and sql_delete1 table to define examples of sql delete as follows.
Select * from sql_delete; Select * from sql_delete1;
- In the below example, we have deleted the specified records by using the where condition. We have used where condition on the id column. We can see that two records will be deleted because where condition will matches the two rows.
DELETE from sql_delete1 where id = 1001; Select * from sql_delete1;
2. In the below example we are using a two-column with where condition to delete records from a table. We are using the id and stud_grade column with the where condition.
DELETE from sql_delete1 where id = 1002 and stud_grade = 1; Select * from sql_delete1;
3. In the below example we are using the comparison operator to delete the records from a table. We are using >, < and = operator to delete the records.
DELETE from sql_delete1 where id > 1005; DELETE from sql_delete1 where id < 1003;
DELETE from sql_delete1 where id = 1004; Select * from sql_delete1;
4. In below example we are deleting the records as per varchar data type column are as follows.
Delete from sql_delete1 where stud_name = ‘ABC’; Select * from sql_delete1;
5. In the below example we are deleting whole the records from the sql_delete1 table as follows.
Delete from sql_delete1; Select * from sql_delete1;
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 Statement. 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 –