SQL - DELETE in SQL
What Is DELETE in SQL?
The DELETE statement in SQL is used to remove one or more rows from a table. It is part of the Data Manipulation Language (DML) and permanently deletes data (unless you're using transactions and rollbacks).
Basic Syntax
DELETE FROM table_name
WHERE condition;
-
table_name: the name of the table you want to delete data from.
-
condition: specifies which rows to delete.
Important: Without WHERE, All Rows Are Deleted!
DELETE FROM employees; -- deletes ALL rows in the table!
Example: Delete a Specific Row
Table: employees
id | name | department |
---|---|---|
1 | Alice | HR |
2 | Bob | IT |
3 | Carol | HR |
DELETE FROM employees
WHERE id = 2;
-
Deletes the row where id = 2 (Bob).
Delete Based on a Condition
DELETE FROM employees
WHERE department = 'HR';
-
Deletes all employees in the HR department.
Delete Using a Subquery
DELETE FROM employees
WHERE id IN (
SELECT employee_id FROM resignations
);
-
Deletes employees who have resigned (based on another table).
DELETE vs TRUNCATE vs DROP
Command | What It Does | Can Use WHERE | Removes Table Structure? |
---|---|---|---|
DELETE | Removes selected rows | ✅ Yes | ❌ No |
TRUNCATE | Removes all rows (faster) | ❌ No | ❌ No |
DROP | Deletes the entire table | ❌ No | ✅ Yes |