DELETE Statement
The DELETE statement removes rows from a table or view. It can be used with a WHERE clause to specify which rows to delete, or without a WHERE clause to delete all rows.
Syntax
DELETE [TOP (expression)]
FROM table_expression
[WHERE ]
[;]
Parameters
- TOP (expression): Limits the number of rows deleted.
- table_expression: The target table or view.
- WHERE condition: Determines which rows are deleted. Omit to delete all rows.
Examples
Delete all rows from a table
DELETE FROM Employees;
Delete rows with a condition
DELETE FROM Employees
WHERE HireDate < '2000-01-01';
Delete the top 10 oldest records
DELETE TOP (10) FROM Orders
WHERE OrderDate < '2015-01-01'
ORDER BY OrderDate ASC;
Remarks
- Use
WHEREto avoid deleting all rows unintentionally. - When deleting from a table with foreign key constraints, ensure related rows are handled appropriately.
- Consider using transactions to rollback if needed.