DELETE with
WHERE
Learn how to surgically remove rows from a database table using the DELETE statement paired with a WHERE clause to target exactly the data you want gone.
What is the DELETE Statement?
In SQL, the DELETE statement removes one or more rows from a table. Unlike SELECT, which only reads data, DELETE permanently changes your database by erasing matching records.
Why WHERE Matters
Without a WHERE clause, DELETE removes every single row in the table. The WHERE clause acts as a filter: it tells the database exactly which rows to target.
When Do You Use DELETE?
The Core Syntax
WHERE condition;
Anatomy of a DELETE Statement
DELETE vs. DROP vs. TRUNCATE
| DELETE | DROP | TRUNCATE | |
|---|---|---|---|
| What it removes | Specific rows (or all) | Entire table structure | All rows only |
| WHERE supported? | Yes | No | No |
| Table still exists? | Yes | No | Yes |
| Can be rolled back? | Yes | Depends on DBMS | Depends on DBMS |
| Type | DML | DDL | DDL |
Step-by-Step Approach
Example 1: Simple Delete
We have a products table and want to remove the item with id = 3.
| id | name | price |
|---|---|---|
| 1 | Notebook | 4.99 |
| 2 | Pen | 1.50 |
| 3 | Eraser | 0.75 |
| 4 | Marker | 2.25 |
WHERE id = 3;
Example 2: Multiple Rows
Delete all students whose grade is below 60 from the students table.
| id | name | grade |
|---|---|---|
| 1 | Alice | 92 |
| 2 | Bob | 45 |
| 3 | Clara | 78 |
| 4 | Dan | 52 |
| 5 | Eva | 88 |
WHERE grade < 60;
Example 3: Compound Conditions
Delete orders from the orders table that are older than 2023 AND have a status of 'cancelled'.
WHERE year < 2023
AND status = 'cancelled';
Example 4: IN and LIKE
You can use advanced operators in WHERE to match multiple values at once or use pattern matching.
DELETE FROM stores
WHERE city IN ('Austin', 'Dallas');
DELETE FROM users
WHERE email LIKE '%@tempmail.com';
The SELECT-First Safety Check
Before running any DELETE, always preview which rows will be affected by running the same WHERE clause with SELECT first.
SELECT * FROM employees
WHERE department = 'Temp';
-- Step 2: If results look correct, delete
DELETE FROM employees
WHERE department = 'Temp';
DELETE Without WHERE
DELETE FROM customers;
Common Mistakes
Summary Cheat Sheet
| Pattern | SQL Syntax | Effect |
|---|---|---|
| Delete one row | DELETE FROM t WHERE id = 5; | Removes 1 row |
| Delete by range | DELETE FROM t WHERE age < 18; | Removes matching rows |
| Delete by list | DELETE FROM t WHERE city IN (...); | Removes listed matches |
| Delete by pattern | DELETE FROM t WHERE name LIKE 'A%'; | Removes pattern matches |
| Delete with AND | DELETE FROM t WHERE a = 1 AND b = 2; | Both must be true |
| Delete ALL rows | DELETE FROM t; | Empties entire table |
Question 1 of 5
Which SQL statement correctly deletes only the row where id equals 7 from the products table?
Question 2 of 5
What happens when you run DELETE FROM employees; without a WHERE clause?
Question 3 of 5
You want to delete all orders from 2020 and 2021. Which WHERE clause is correct?
Question 4 of 5
Before running a DELETE, what is the recommended safety practice?
Question 5 of 5
Which statement deletes users whose email ends with '@spam.com'?