The SQL DELETE Statement is used to delete one or more existing records from a table or a view. The basic syntax of SQL Server delete statement is as shown below:
-- SQL Server DELETE Syntax DELETE FROM [Table_Name] WHERE Condition
- Table_Name: Fully qualifies Table name from which you want to delete the records
- Condition: Here, we have to give the filters or conditions. If the condition is TRUE, then only the SQL Server DELETE Statement will delete the records.
Here, we are going to perform the different types of delete operations on this SQL Server table, which holds 15 records
SQL Delete Single record
In this example, 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
From the below screenshot you can observe that it had 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
The above query will delete all the records, whose Education is Partial High School, and Yearly Income is 45000
SQL Delete Top Clause
USE [SQL Tutorial] GO DELETE TOP (1) FROM [SQL Delete] WHERE [YearlyIncome] <= 50000
The above query will check for the records whose yearly income is less than or equal to 5000, and then TOP Clause will select the First record.
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 following screenshot will show you the data inside the Department table.
DELETE del FROM [SQL Delete] AS del INNER JOIN Department AS dept ON del.[DeptID] = dept.id WHERE dept.DepartmentName = 'Sr. Software Developer'
From the below screenshot, you can observe that it had deleted all the records, whose department name is Sr. Software Developer. Remember, the Department name is coming from the Department Table.
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' )
The following screenshot will delete 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, first Navigate to the required Database and then select the required Table. Right Click on the Table will open the Context Menu with different options
Select Script Table as -> Delete To -> New Query Editor Window
Once you selected the New Query Editor Window, SQL will generate a delete statement for the selected table as we shown below
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
From the below code snippet you can see that, 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 (Execute 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
Before we begin Sql Server Delete Is Null demonstrating this example, let me show you the data
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