How to perform a DELETE in SQL Server

Written by Johannes Åkesson
Reviewed by Johannes Åkesson
Aug 01, 2024
3 min read

Deleting data in SQL Server is a critical operation for business users who need to remove obsolete or incorrect records from their databases. This guide will walk you through the process of using the DELETE statement in SQL Server, providing comprehensive steps and code snippets you can use directly in SQL Server Management Studio (SSMS).

Understanding the SQL DELETE Statement

The DELETE statement in SQL Server is used to remove one or more rows from a table. The basic syntax for the DELETE statement is:

DELETE FROM table_name

WHERE condition;

Steps to Perform a DELETE in SQL Server

1. Identify the Table and Records to Delete: Determine which table and records you need to delete. Ensure you have the necessary permissions to delete the data.

2. Write the DELETE Statement: Formulate the DELETE statement with the appropriate WHERE clause to specify which records to delete.

3. Execute the Statement: Run the statement in SSMS to remove the records.

Example 1: Deleting a Single Row

Let’s start with a simple example where we delete a single row from the Employees table.

DELETE FROM Employees

WHERE EmployeeID = 1;

Example 2: Deleting Multiple Rows

You can also delete multiple rows based on certain conditions. Suppose we want to delete all employees in the Sales department.

DELETE FROM Employees

WHERE Department = 'Sales';

Example 3: Deleting All Rows from a Table

If you need to delete all rows from a table but keep the table structure intact, you can use the DELETE statement without a WHERE clause. However, be cautious with this operation as it removes all data from the table.

DELETE FROM Employees;

Example 4: Using a Subquery in a DELETE Statement

You can use a subquery to delete rows based on a condition in another table. Suppose we have a table Departments and we want to delete employees whose departments have been closed.

DELETE FROM Employees

WHERE DepartmentID IN (SELECT DepartmentID FROM Departments WHERE Status = 'Closed');

Example 5: Using OUTPUT Clause with DELETE

The OUTPUT clause can be used to return information about the deleted rows. This can be helpful for auditing or logging purposes.

DELETE FROM Employees

OUTPUT DELETED.EmployeeID, DELETED.FirstName, DELETED.LastName

WHERE Department = 'Sales';

Best Practices for Using DELETE Statements

1. Always Backup Your Data: Before performing any delete operations, ensure you have a backup of your data to prevent accidental data loss.

2. Test Your Queries: Run your delete queries in a test environment to make sure they perform as expected without unintended side effects.

3. Use Transactions: For critical delete operations, use transactions to ensure that your deletes are atomic and can be rolled back if something goes wrong.

BEGIN TRANSACTION;

DELETE FROM Employees

WHERE Department = 'Sales';

-- If everything is correct

COMMIT TRANSACTION;

-- If something went wrong

-- ROLLBACK TRANSACTION;

4. Double-Check Your WHERE Clause: Be cautious with the WHERE clause to avoid deleting more rows than intended. Always double-check your conditions.

Why not try deleting data from SQL Server using Excel?

Deleting data in SQL Server is a straightforward process once you understand the syntax and best practices and if you have the right technical skills in SQL Server.

If you don’t have the technical skills, you could try the SQL Spreads Excel Add-In. It is easy to connect to your SQL Server database from within Excel using the Add-In. Once connected, you are able to easily delete data in SQL Server. Find out more about the SQL Spreads Excel Add-In or download the free 14-day trial immediately.

For more tips and detailed guides on SQL Server, visit our SQL Spreads blog.

Related Articles

Try SQL Spreads for free

Try SQL Spreads during 14 days for free. The free trial is the fully
functional, time-limited only product.
Try Excel Add-In for Free download-image
stars-image
14-days Free Trial. No credit card required.