The SQL DELETE Statement deletes one or more existing records from a table or a view. The syntax of SQL Server delete statement is
-- SQL Server DELETE Syntax DELETE FROM [Table_Name] WHERE Condition
- Table_Name: Fully qualifies Table name to delete the records
- Condition: Provide the filters or conditions. If the condition is TRUE, then only the SQL Server DELETE Statement will delete the records.
The SQL Server table table to perform the different types of delete operations is
SQL Delete Single record
We are going to delete a single record from [SQL Delete] Table. For this, we are using the Where Clause to restrict the Delete Statement to delete a single record.
USE [SQL Tutorial] GO DELETE FROM [SQL Delete] WHERE [EmpID] = 2
It deleted the employee record, whose ID value = 103
SQL Delete Multiple records
In this Delete Multiple records example, We use AND Operator in Where Clause to check multiple conditions before it starts deleting a record.
DELETE FROM [SQL Delete] WHERE [Education] = 'Partial High School' AND [YearlyIncome] = 45000
Deleted all the records, whose Education is Partial High School, and Yearly Income is 45000
SQL Delete Top Clause
In this Sql Server delete statement example, we delete the first record from the table, whose yearly income is less than or equal to 5000. For this Sql Server example, we are using the TOP Clause.
USE [SQL Tutorial] GO DELETE TOP (1) FROM [SQL Delete] WHERE [YearlyIncome] <= 50000
It will check for the records whose yearly income is less than or equal to 5000, and then TOP Clause will select the First record.
Delete Join
Let me delete the records from [SQL Delete] Table based on the Department name in another table. For this, we are using Inner Join along with the SQL delete statement to join two tables. The data inside the Department table.
Delete Statement CODE
DELETE del FROM [SQL Delete] AS del INNER JOIN Department AS dept ON del.[DeptID] = dept.id WHERE dept.DepartmentName = 'Sr. Software Developer'
It deleted all the records, whose department name is Sr. Software Developer. Remember, the Department name is coming from the Department Table.
Delete subquery
In this SQL delete statement with a subquery example, let us delete the records from [SQL Delete] Table based on the Department name in another table. For this, we are using Subquery.
DELETE FROM [SQL Delete] WHERE [DeptID] IN ( SELECT id FROM Department WHERE DepartmentName = 'Module Lead' )
Deleted all the records from [SQL Delete] table, whose department name is Module lead.
SQL Delete All Columns
In this Sql Server delete statement example, We are going to delete all the Columns
USE [SQL Tutorial] GO DELETE FROM [SQL Delete]
NOTE: If you accidentally forgot the Where Clause, then you will end up deleting all the records from Source.
Delete Statement using Management Studio
If you can access the Management Studio in SQL Server, use the Intellisense to write this Delete Statement. To do so, Right Click on the Table -> Select Script Table as -> Delete To -> New Query Editor Window
SQL will generate a delete statement for the selected table
and the Code
DELETE FROM [dbo].[SQL Delete] WHERE <Search Conditions,,> GO
Let us add the Search Condition
DELETE FROM [dbo].[SQL Delete] WHERE [EmpID] = 1
SQL Server Delete Stored Procedure
Let us see how to use the SQL Server Delete Statement inside a Stored procedure. Here, we are deleting records whose Occupation equals to Clerical or their yearly income is less than or equal to 60000
-- SQL Server Delete Stored Procedure IF OBJECT_ID ( 'sp_DeleteEmpRecords', 'P' ) IS NOT NULL DROP PROCEDURE sp_DeleteEmpRecords; GO CREATE PROCEDURE sp_DeleteEmpRecords AS BEGIN SET NOCOUNT ON; DELETE FROM [SQL Delete] WHERE Occupation = 'Clerical' OR YearlyIncome <= 60000 END GO
Let me use EXEC Command to execute the Delete stored procedure
EXEC dbo.sp_DeleteEmpRecords GO
Now, let us see whether the execution of the stored procedure removed or deleted the records in our table or not
-- Select all records from a Table after executing SP SELECT [EmpID], [FirstName], [LastName], [Education], [Occupation], [YearlyIncome], [Sales], [HireDate], [DeptID] FROM [SQL Delete]
Delete Between example
This Sql Server delete statement example uses the BETWEEN Operator to delete the records between two dates.
DELETE FROM [SQL Delete] WHERE [HireDate] BETWEEN '2016-01-27- AND '2007-01-28'
Above query will delete all the records whose Hire date is between 2006-01-27 and 2007-01-28
SQL Delete If Exists example
While deleting a record from a table, it is always a good practice to check whether the records exist or not.
Here, we are going to use the EXISTS Operator to check the records in Delete table against the department table. If they exist and their last name is ‘Tutorial Gateway’, then delete that record.
DELETE FROM [SQL Delete] WHERE EXISTS ( SELECT * FROM Department WHERE [SQL Delete].DeptID = Department.id AND [SQL Delete].LastName = 'Tutorial Gateway'
and the remaining data view is
Delete IS Null example
Data that we used for this Sql Server Delete Is Null demonstrating is
In this delete command example, we are going to delete all the employees whose Age is NULL. For this, we used the IS NULL operator.
DELETE FROM [SQL Delete] WHERE [Age] IS NULL
and the remaining data is