DELETE (Transact‑SQL)
On this page
Syntax
DELETE [TOP (expression) [PERCENT]] FROM { <object> | <derived_table> | <table_variable> } [WITH ( <table_hint> [ ,…n ] )] [WHERE <search_condition> ] [OPTION ( <query_hint> [ ,…n ] ) ]
The DELETE
statement removes rows from a table or view.
Parameters
Parameter | Description |
---|---|
TOP (expression) [PERCENT] | Limits the number of rows deleted. |
FROM <object> | The target table, view, or derived table. |
WITH (table_hint) | Specifies table hints such as ROWLOCK . |
WHERE <search_condition> | Filters the rows to be deleted. |
OPTION (query_hint) | Applies query hints like MAXDOP . |
Examples
Delete all rows from a table
DELETE FROM dbo.Customers;
Delete rows with a condition
DELETE FROM dbo.Orders WHERE OrderDate < '2020-01-01';
Delete the top 10 rows
DELETE TOP (10) FROM dbo.Logs WHERE LogLevel = 'Debug';
Delete using a join
DELETE t FROM dbo.Employee AS e JOIN dbo.Timesheet AS t ON e.EmployeeID = t.EmployeeID WHERE e.Status = 'Terminated';
Remarks
- DELETE without a
WHERE
clause removes all rows but retains table structure and constraints. - When deleting from a view, the underlying table must support DELETE operations.
- Use
OUTPUT
clause to capture deleted rows (not shown here). - Transactions can be used to ensure atomicity of delete operations.