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

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.

We use this table to perform the different operation types of delete in SQL Server is

SQL DELETE Statement 1

SQL Delete Single record

We are going to delete a single record from the Table. For this, we are using the Where Clause to restrict this Statement to eliminate a single record. If you omit the where clause, it will delete all rows in a table.

DELETE FROM [SQLDelete]
  WHERE [EmpID] = 2

It eliminated 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 removing a record.

DELETE FROM [SQLDelete]
  WHERE [Education] = 'Partial High School' AND
        [YearlyIncome] = 45000

Remove 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 remove 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 SQL Select TOP.

DELETE TOP (1) 
FROM [SQLDelete]
  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

Let me remove the records from Table based on the Department name in another table. For this, we are using Inner Join along with the delete statement to join two tables. The data inside the Department table.

SQL DELETE Statement 11

CODE

DELETE del
FROM [SQLDelete] AS del
INNER JOIN
      Department AS dept ON
  del.[DeptID] = dept.id
WHERE dept.DepartmentName = 'Sr. Software Developer'

It removed all the records, whose department name is Sr. Software Developer. Remember, the Department name is coming from the Department Table.

SQL DELETE Statement 5

Subquery

In this SQL delete statement with a subquery example, let us take out the rows from Table based on the Department name in another table. For this, we are using Subquery.

DELETE FROM [SQLDelete]
WHERE [DeptID] IN
 (
   SELECT id FROM Department
     WHERE DepartmentName = 'Module Lead'
 )

Eliminating all the records from [SQLDelete] table, whose department name is Module lead.

SQL DELETE Statement 6

Removing All Columns

In this Sql Server delete statement example, We are going to take out all the Columns

DELETE FROM [SQLDelete]

NOTE: If you accidentally forgot the Where Clause, then you will end up removing all the records from Source.

Using Management Studio

If you can access the Management Studio in SQL Server, use the Intellisense to write this Statement. To do so, Right Click on the Table -> Select Script Table as -> DeleteTo -> New Query Editor Window

SQL DELETE Statement 7

It will generate the statement for the selected table and the Code is

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

Let us add the Search Condition

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

SQL Server Delete Stored Procedure

Let us see how to use this Statement inside a Stored procedure. Here, we are removing records whose Occupation equals to Clerical or their yearly income is less than or equal to 60000

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 [SQLDelete]
		WHERE Occupation = 'Clerical' OR
			YearlyIncome <= 60000
	END
GO
Messages
-------
Command(s) completed successfully.

Let me use EXEC Command to execute the stored procedure

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

Now, let us see whether the execution of the stored procedure removed the rows 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 [SQLDelete]
SQL DELETE Statement 14

Between example

This Sql Server delete statement example uses the BETWEEN Operator to remove the records between two dates.

DELETE 
FROM [SQLDelete]
WHERE [HireDate] BETWEEN '2016-01-27- AND '2007-01-28'
Messages
-------
(3 row(s) affected)

Above query will remove 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 removing 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 the DeleteTable against the department table. If they exist and their last name is ‘Tutorial Gateway’, then remove that record.

DELETE FROM [SQLDelete]
WHERE EXISTS (
      SELECT * FROM Department
      WHERE [SQLDelete].DeptID = Department.id
      AND [SQLDelete].LastName = 'Tutorial Gateway'
Messages
-------
(4 row(s) affected)

and the remaining data view is

SQL Delete If exists 2

SQL Delete IS Null example

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

SQL Delete Is Null

In this example, we are going to remove all the employees whose Age is NULL. For this, we used the IS NULL operator.

DELETE 
FROM [SQLDelete]
WHERE [Age] IS NULL
Messages
-------
(4 row(s) affected)

and the remaining data is

SQL Delete Is Null 2