Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

SQL DELETE Statement

by suresh

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

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

SQL DELETE Statement 8

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

Let me use EXEC Command to execute the Delete stored procedure

EXEC dbo.sp_DeleteEmpRecords
GO
SQL DELETE Statement 13

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'
SQL Delete Between 1

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'
SQL Delete If exists 1

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
SQL Delete Is Null 1

and the remaining data is

SQL Delete Is Null 2

Placed Under: SQL

  • Install SQL Server
  • Install SQL Management Studio
  • Uninstall Management Studio
  • Install AdventureWorks Database
  • SQL Management Studio Intro
  • Connect SQL with sqlcmd utility
  • SQL Attach Database
  • SQL Detach Database
  • SQL Restore Database
  • Restore Database using BAK
  • SQL Rename Database with Files
  • Get SQL Database Names
  • SQL Create Table
  • SQL Rename Table
  • SQL Alter Table
  • SQL Add Column
  • SQL Rename Column
  • Get SQL Table Names in a DB
  • Find SQL Table Dependencies
  • Rename SQL Table & Column
  • SQL Global & Local Temp Table
  • SQL Table Variable
  • SQL Derived Table
  • SQL DATALENGTH
  • SQL Data Types
  • DML, DDL, DCL & TCL Cmds
  • SQL Query Builder
  • SQL ALIAS
  • SQL SELECT Statement
  • SQL SELECT DISTINCT
  • SQL SELECT INTO Statement
  • SQL INSERT Statement
  • SQL INSERT INTO SELECT
  • SQL BULK INSERT or BCP
  • SQL UPDATE Statement
  • SQL UPDATE from SELECT
  • SQL DELETE Statement
  • SQL TRUNCATE Table
  • SQL CASE Statement
  • SQL MERGE Statement
  • SQL Subquery
  • SQL CTE
  • SQL PIVOT
  • SQL UNPIVOT
  • SQL Clauses Examples
  • SQL TOP Clause
  • SQL WHERE Clause
  • SQL ORDER BY Clause
  • SQL GROUP BY Clause
  • SQL HAVING Clause
  • SQL Primary Key
  • SQL Foreign Key
  • SQL Referential Integrity
  • SQL Check Constraint
  • SQL Unique Constraint
  • SQL Default Constraint
  • SQL Clustered Index
  • SQL Non Clustered Index
  • SQL Filtered Indexes
  • SQL COALESCE Function
  • SQL IS NOT NULL
  • SQL IS NULL Function
  • SQL ISNULL
  • SQL JOINS
  • SQL CROSS JOIN
  • SQL FULL JOIN
  • SQL SELF JOIN
  • SQL Outer Joins
  • SQL Cross Join Vs Inner Join
  • SQL LEFT JOIN
  • SQL RIGHT JOIN
  • SQL AND & OR Operators
  • SQL Arithmetic Operators
  • SQL BETWEEN Operator
  • SQL Comparison Operators
  • SQL LIKE
  • SQL EXCEPT
  • SQL EXISTS Operator
  • SQL NOT EXISTS Operator
  • SQL INTERSECT
  • SQL IN Operator
  • SQL NOT IN Operator
  • SQL UNION
  • SQL UNION ALL
  • SQL IF ELSE
  • SQL ELSE IF
  • SQL WHILE LOOP
  • SQL Nested While Loop
  • SQL BREAK Statement
  • SQL CONTINUE Statement
  • SQL GOTO Statement
  • SQL IIF Function
  • SQL CHOOSE Function
  • SQL Change Data Capture
  • SQL Table Partitioning
  • SQL Table Partition using SSMS
  • SQL TRY CATCH
  • SQL VIEWS
  • SQL User Defined Functions
  • SQL Alter User Defined Functions
  • SQL Stored Procedure Intro
  • Useful System Stored Procedures
  • SQL SELECT Stored Procedure
  • SQL INSERT Stored Procedure
  • SQL UPDATE Stored Procedure
  • Stored Procedure Return Values
  • Stored Procedure Output Params
  • Stored Procedure Input Params
  • Insert SP result into Temp Table
  • SQL Triggers Introduction
  • SQL AFTER INSERT Triggers
  • SQL AFTER UPDATE Triggers
  • SQL AFTER DELETE Triggers
  • SQL INSTEAD OF INSERT
  • SQL INSTEAD OF UPDATE
  • SQL INSTEAD OF DELETE
  • SQL STATIC CURSOR
  • SQL DYNAMIC CURSOR
  • SQL FORWARD_ONLY Cursor
  • SQL FAST_FORWARD CURSOR
  • SQL KEYSET CURSOR
  • SQL TRANSACTIONS
  • SQL Nested Transactions
  • SQL ACID Properties
  • Create SQL Windows Login
  • Create SQL Server Login
  • SQL Server Login Error
  • Create SQL Server Roles
  • SQL Maintenance Plan
  • Backup SQL Database
  • SQL Ranking Functions Intro
  • SQL RANK Function
  • SQL PERCENT_RANK Function
  • SQL DENSE_RANK Function
  • SQL NTILE Function
  • SQL ROW_NUMBER
  • SQL Aggregate Functions
  • SQL Date Functions
  • SQL Mathematical Functions
  • SQL String Functions
  • SQL CAST Function
  • SQL TRY CAST
  • SQL CONVERT
  • SQL TRY CONVERT
  • SQL PARSE Function
  • SQL TRY_PARSE Function
  • SQL Calculate Running Total
  • SQL Find Nth Highest Salary
  • SQL Reverse String
  • SQL FOR XML PATH
  • SQL FOR XML AUTO
  • SQL FOR XML RAW

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy