DELETE (Transact-SQL)

Removes one or more rows from a table or view.

Syntax

DELETE
[ FROM ]
[ .. ]
[  ]
[  ]
[ WHERE  ]
[ OPTION (  [ ,...n ] ) ]

-- For SQL Server 2008 and later, you can also use the OUTPUT clause.
[ OUTPUT  INTO  ]

FROM
[ .. ] [  ]
[ ,...n ]

WHERE
[ NOT ] 

Arguments

Description

The DELETE statement is used to remove records from a database table. You can delete single rows, multiple rows, or all rows from a table.

When deleting from a table that has foreign key constraints referencing it, you must ensure that no rows in the referencing tables point to the rows being deleted. Otherwise, the deletion will fail unless the foreign key constraint is set to ON DELETE CASCADE or ON DELETE SET NULL.

Example 1: Deleting a specific row

This example deletes the row from the Products table where the ProductID is 7.

DELETE FROM Production.Product
WHERE ProductID = 7;

Example 2: Deleting multiple rows based on a condition

This example deletes all products from the Products table that have a ListPrice less than $10.

DELETE FROM Production.Product
WHERE ListPrice < 10.00;

Example 3: Deleting rows using a JOIN

This example deletes orders from the SalesOrderHeader table that were placed by customers in the 'Europe' territory.

DELETE soh
FROM Sales.SalesOrderHeader AS soh
INNER JOIN Sales.Customer AS c
    ON soh.CustomerID = c.CustomerID
INNER JOIN Person.CountryRegion AS cr
    ON c.CountryRegionCode = cr.CountryRegionCode
WHERE cr.Name = 'Europe';

Example 4: Deleting all rows from a table

This statement deletes all rows from the SalesOrderDetail table. This is equivalent to using the TRUNCATE TABLE statement, but DELETE logs each row deletion.

DELETE FROM Sales.SalesOrderDetail;

Note

Using DELETE without a WHERE clause will remove all rows from the table. This operation can be very time-consuming and resource-intensive for large tables. Consider using TRUNCATE TABLE for a faster way to remove all rows, as it is minimally logged.

Tip

When deleting a large number of rows, consider performing the delete operation in batches to reduce the transaction log size and minimize the impact on system performance.

Permissions

Requires DELETE permission on the table.

See Also