SQL DELETE Statement

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

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 Statement 1

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 Statement 2

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 Statement 3

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.

SQL DELETE Statement 4

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.

SQL DELETE Statement 11

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.

SQL DELETE Statement 5

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 Statement 6

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 DELETE Statement 7

SQL will generate a delete statement for the selected table and the Code is

DELETE FROM [dbo].[SQL Delete]
      WHERE <Search Conditions,,>
GO

Let us add the Search Condition

DELETE FROM [dbo].[SQL Delete]
      WHERE [EmpID] = 1
SQL DELETE Statement 9

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
Messages
-------
Command(s) completed successfully.

Let me use EXEC Command to execute the Delete stored procedure

EXEC dbo.sp_DeleteEmpRecords
GO
Messages
-------
Command(s) completed successfully.

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]
SQL DELETE Statement 14

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'
Messages
-------
(3 row(s) affected)

Above query will delete all the records whose Hire date is between 2006-01-27 and 2007-01-28

SQL Delete Between 2

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'
Messages
-------
(4 row(s) affected)

and the remaining data view is

SQL Delete If exists 2

Delete IS Null example

Data that we used for this Sql Server Delete Is Null demonstrating is

SQL Delete Is Null

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
Messages
-------
(4 row(s) affected)

and the remaining data is

SQL Delete Is Null 2