- Aggregate functions
- AVERAGE function
- BETWEEN operator
- CASE expression
- CAST() function
- COALESCE() function
- Comment
- Common table expression
- Constraints
- CONVERT function
- Cursor
- Data types
- Date functions
- DELETE statement
- DROP TABLE statement
- EXISTS operator
- HAVING clause
- IF statement
- Index
- IS NOT NULL condition
- IS NULL condition
- Joins
- LAG function
- LENGTH() function
- LIKE operator
- MERGE statement
- Normalization
- Not equal
- Operators
- ORDER BY clause
- Partition
- Pivot table
- Regex
- REPLACE function
- ROUND function
- SELECT DISTINCT clause
- SELECT statement
- Set operators
- Stored procedure
- String functions
- Subquery
- Substring
- Temporary table
- Transaction
- Trigger
- TRUNCATE TABLE
- UPDATE statement
- Views
- WHERE clause
- Window functions
SQL
SQL DELETE Statement: Syntax, Usage, and Examples
The SQL DELETE statement removes one or more rows from a table based on a condition. It permanently deletes data, making it a powerful but potentially dangerous command if not used carefully.
How to Use the SQL DELETE Statement
The basic syntax of a SQL DELETE statement is:
DELETE FROM table_name
WHERE condition;
You can use it to delete specific rows that meet the condition. Without a WHERE
clause, it removes all rows from the table:
DELETE FROM Customers
WHERE Country = 'Canada';
Use this if you want to remove only customers from a specific country.
To delete everything in a table:
DELETE FROM Orders;
Use this only when you intend to clear out all the data while keeping the table structure intact.
In SQL Server, you can also delete with joins:
DELETE Orders
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID
WHERE Customers.Country = 'France';
You can target related records this way when working across multiple tables.
When to Use the DELETE Statement in SQL
Use the SQL DELETE statement when you need to remove data while keeping the table schema and other records intact. Here are several common scenarios:
Cleaning Up Old Data
Over time, tables fill up with outdated records—cancelled orders, expired accounts, or logs. Deleting these can help improve performance and reduce storage.
Managing User Requests
If users can delete their accounts or data entries, the SQL DELETE statement helps remove their data on request while leaving other users untouched.
Correcting Mistakes
Sometimes incorrect data slips in. You can use a condition to delete only the bad records while leaving the rest of the table alone.
Examples of the SQL DELETE Statement
Let’s walk through different examples where the DELETE statement plays a role in real-world data handling.
Example 1: Delete by Condition
DELETE FROM Employees
WHERE Department = 'HR';
Use this to clean out employees who no longer belong to a specific department.
Example 2: Delete a Single Row by ID
DELETE FROM Products
WHERE ProductID = 77;
This is a safe and common pattern for deleting specific items using a primary key.
Example 3: Delete All Rows from a Table
DELETE FROM Sales;
Use this only if you're wiping the slate clean and don't need any of the existing rows.
Example 4: Delete With a Subquery
DELETE FROM Orders
WHERE CustomerID IN (
SELECT CustomerID FROM Customers WHERE Country = 'Mexico'
);
You can use subqueries to find matching rows in another table and delete based on the result.
Example 5: Delete Based on Join (SQL Server)
DELETE T
FROM Transactions T
INNER JOIN Users U ON T.UserID = U.UserID
WHERE U.Status = 'Inactive';
Use joins when deleting records that depend on another table’s condition.
Learn More About the DELETE Statement in SQL
DELETE vs TRUNCATE vs DROP
Understanding the difference between these SQL commands is crucial. The DELETE
statement removes data row-by-row and can include a condition. TRUNCATE
deletes all rows without logging individual row deletions and is faster but irreversible. DROP
removes the entire table structure.
Use DELETE when you want control over which rows get removed. Use TRUNCATE when you want to clear all data quickly and don’t need to track what was removed. Use DROP if you’re removing the table entirely.
SQL Server DELETE Statement Variations
In SQL Server, you can use TOP
to limit how many rows get deleted:
DELETE TOP (10)
FROM Logs
WHERE ErrorLevel = 'Warning';
This can be helpful for deleting in chunks without locking the entire table.
You can also use the OUTPUT
clause to return information about the rows you deleted:
DELETE FROM Inventory
OUTPUT DELETED.ProductID, DELETED.Quantity
WHERE Quantity = 0;
Use the OUTPUT clause to log or review what you’ve deleted without needing to run a SELECT
first.
Using DELETE in Transactions
Deleting records can be risky. In SQL Server and most relational databases, you can wrap the DELETE statement in a transaction to test before committing:
BEGIN TRANSACTION;
DELETE FROM Orders WHERE OrderDate < '2022-01-01';
-- Rollback if you made a mistake
-- ROLLBACK;
-- Commit if everything looks good
COMMIT;
Using transactions gives you a safety net. Always test with a SELECT before deleting.
Handling Foreign Key Constraints
If a table has foreign key relationships, deleting rows can result in constraint violations. You need to either delete child rows first or define ON DELETE CASCADE
in your schema.
DELETE FROM Orders
WHERE CustomerID = 123;
If the Orders table has child records (like order items), you may not be able to delete this without addressing the dependencies.
Performance Considerations
The DELETE statement logs each row deletion, which can slow things down if you're deleting millions of rows. If you're performing bulk deletions, consider breaking them into batches:
WHILE 1=1
BEGIN
DELETE TOP (1000)
FROM Logs
WHERE CreatedAt < '2023-01-01';
IF @@ROWCOUNT = 0
BREAK;
END
This pattern lets you delete gradually without locking the entire table.
Use the SQL DELETE statement to remove targeted rows from a table, clean up data, or reset records in response to user activity. It’s one of the most common and powerful tools in SQL, so handle it with care—especially when skipping the WHERE clause. Always review the rows you intend to delete with a SELECT first, and use transactions when dealing with critical or large datasets.
Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.