DELETE Statements
Syntax
DELETE FROM table_name
[WHERE condition]
[;]
The DELETE
statement removes rows from a table based on an optional WHERE
clause.
Basic Example
-- Remove all rows where the status is 'inactive'
DELETE FROM Employees
WHERE Status = 'inactive';
Deleting All Rows
-- Delete every row from the table (use with caution!)
DELETE FROM Orders;
Without a WHERE
clause, every row is removed but the table structure remains.
Using Joins
-- Delete orders that belong to customers in a specific region
DELETE o
FROM Orders o
JOIN Customers c ON o.CustomerID = c.CustomerID
WHERE c.Region = 'West';
Best Practices
- Always back up data before performing bulk deletes.
- Test the
WHERE
clause with aSELECT
first. - Consider using transactions to allow rollback.
- Index columns used in the
WHERE
clause for performance.