01 / 21

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.

SQL

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.

SELECT
Reads data. Table stays unchanged.
DELETE
Removes rows. Table is permanently altered.

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.

DELETE FROM table; Row 1 — DELETED Row 2 — DELETED Row 3 — DELETED ALL rows gone DELETE ... WHERE id = 2; Row 1 — kept Row 2 — DELETED Row 3 — kept Only matching row removed

When Do You Use DELETE?

Account Deletion
A user requests their account be removed from the system.
Data Cleanup
Old, expired, or test records need to be purged.
Error Correction
A duplicate or incorrect entry was inserted by mistake.

The Core Syntax

DELETE FROM table_name
WHERE condition;
DELETE FROM
Keyword pair that starts the command, followed by the table name.
WHERE
Filter clause: only rows matching this condition are deleted.

Anatomy of a DELETE Statement

DELETE FROM students WHERE grade = 'F' ; Action Keyword Tells SQL to remove rows Table Name Which table to modify Filter Condition Which rows to target Without WHERE, ALL rows in the table are deleted!

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

1
Identify the table. Which table contains the rows you need to remove?
2
Write a SELECT first. Use SELECT * FROM table WHERE condition to preview which rows will be affected.
3
Build the WHERE clause. Define the condition that precisely targets the rows to delete.
4
Replace SELECT * with DELETE. Change your SELECT query to DELETE FROM table WHERE condition.
5
Execute and verify. Run the query and confirm the correct number of rows were removed.

Example 1: Simple Delete

We have a products table and want to remove the item with id = 3.

idnameprice
1Notebook4.99
2Pen1.50
3Eraser0.75
4Marker2.25
DELETE FROM products
WHERE id = 3;

Example 2: Multiple Rows

Delete all students whose grade is below 60 from the students table.

idnamegrade
1Alice92
2Bob45
3Clara78
4Dan52
5Eva88
DELETE FROM students
WHERE grade < 60;

Example 3: Compound Conditions

Delete orders from the orders table that are older than 2023 AND have a status of 'cancelled'.

DELETE FROM orders
WHERE year < 2023
  AND status = 'cancelled';
AND
Both conditions must be true for a row to be deleted.
OR
Either condition being true causes the row to be deleted.

Example 4: IN and LIKE

You can use advanced operators in WHERE to match multiple values at once or use pattern matching.

-- Delete specific cities
DELETE FROM stores
WHERE city IN ('Austin', 'Dallas');
-- Delete emails matching a pattern
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.

-- Step 1: Preview what will be deleted
SELECT * FROM employees
WHERE department = 'Temp';

-- Step 2: If results look correct, delete
DELETE FROM employees
WHERE department = 'Temp';

DELETE Without WHERE

-- DANGER: This deletes EVERY row!
DELETE FROM customers;
Before Row 1: Alice Row 2: Bob Row 3: Clara Row 4: Dan 4 rows DELETE After EMPTY 0 rows remain

Common Mistakes

Forgetting WHERE
Running DELETE FROM table; wipes out every row. Always include a WHERE clause.
Wrong Comparison
Using = instead of LIKE for pattern matches, or mixing up < and > operators.
No Preview First
Skipping the SELECT step before DELETE. You lose data you cannot recover.

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'?