SQL Delete
The DELETE statement removes existing rows from a table.
Removing rows with DELETE
The DELETE statement removes whole rows from a table. You name the table and use a WHERE clause to say which rows should go. DELETE always removes entire rows; if you only want to clear a single field, you use UPDATE to set that field to NULL instead.
Deleting specific rows
-- Remove one employee by id
DELETE FROM Employees
WHERE employee_id = 101;
-- Remove every employee in a closed department
DELETE FROM Employees
WHERE department = 'Legacy';The WHERE clause controls the damage
Just as with UPDATE, the WHERE clause is what limits a delete to the rows you actually mean to remove. A precise condition removes one row, a broad condition removes many, and no condition removes them all. Because deleted rows are gone, it pays to be careful here.
A safe way to preview a delete
Before running a delete, it is a good habit to run a SELECT with the same WHERE clause first. Whatever rows that SELECT returns are exactly the rows the DELETE will remove. This lets you confirm your condition targets the right data before anything is lost.
Preview, then delete
-- 1. See which rows match
SELECT * FROM Employees
WHERE department = 'Legacy';
-- 2. If the list looks right, remove them
DELETE FROM Employees
WHERE department = 'Legacy';DELETE compared with DROP and TRUNCATE
It helps to know how DELETE differs from two related commands. DELETE removes rows but keeps the table itself, and it can be filtered with WHERE. TRUNCATE quickly removes all rows but keeps the empty table. DROP removes the table structure entirely.
- DELETE removes whole rows and leaves the table in place.
- TRUNCATE empties all rows at once but keeps the table.
- DROP TABLE removes the table and its structure completely.
- To clear a single field instead of a row, use UPDATE ... SET column = NULL.
Exercise: SQL Delete
What does the DELETE statement do?