Learn SQL
-- modifying.delete

DELETE — Removing Rows in SQL

Delete rows, not tables — WHERE is mandatory thinking

--concept

DELETE removes whole rows matching a WHERE condition. Without WHERE it empties the entire table (still logging every removed row). Related sibling: TRUNCATE empties a table instantly without row-by-row logging, and DROP TABLE deletes the table structure itself. As with UPDATE, preview the condition with SELECT first.

--how it works
1

Target rows with WHERE

DELETE FROM employees WHERE department = 'Marketing' — the condition grammar is the same as in SELECT.

2

Preview the hit list

Run the identical condition as a SELECT first — you cannot undelete what you never backed up.

3

Whole rows go

DELETE removes entire rows; you cannot delete "a column's value" — set it to NULL instead.

4

Know the no-WHERE version

DELETE FROM employees empties the table, row by row, logged. TRUNCATE does it instantly but cannot be filtered.

5

Mind foreign keys

Deleting a referenced parent may block (RESTRICT) or cascade (CASCADE) — know which your schema chose.

--visualization
WHERE department =1 row(s) will go
idnamedepartmentsalary1AliceEngineering850002BobSales520003CynthiaEngineering910004DavidMarketing470005ElenaSales580006FaridEngineering73000DELETE removes whole rows · TRUNCATE empties everything · DROP deletes the table itself
marked for deletion
Stage 1 / 3
DELETE FROM employees WHERE department = 'Marketing' — preview first: how many rows match?
--example
query.sql
1DELETE FROM employees
2WHERE department = 'Marketing';
Result
idnamedepartmentsalaryage
1AliceEngineering8500029
2BobSales5200035
3CynthiaEngineering9100041
5ElenaSales5800026
6FaridEngineering7300033

David’s Marketing row is removed; the remaining five rows shift up. Run the visualization and watch the doomed row dissolve while survivors close the gap.

--variants

Delete a group

Remove every Marketing row.

query.sql
1DELETE FROM employees
2WHERE department = 'Marketing';
Result
idnamedepartmentsalaryage
1AliceEngineering8500029
2BobSales5200035
3CynthiaEngineering9100041
5ElenaSales5800026
6FaridEngineering7300033
1One row matches: David.
2The engine removes it; the remaining five close the gap.
3"1 row deleted" — verify the count before trusting the result.

Delete one by key

The safest delete — a unique key.

query.sql
1DELETE FROM employees WHERE id = 4;
Result
idnamedepartmentsalaryage
1AliceEngineering8500029
2BobSales5200035
3CynthiaEngineering9100041
5ElenaSales5800026
6FaridEngineering7300033
1Primary key lookup, one doomed row.
2Same outcome as the department version, but impossible to over-match.
3Production bulk-deletes should still be batched and transaction-wrapped.

The empty table

No WHERE = every row goes.

query.sql
1DELETE FROM employees;
2-- table now has 0 rows (structure remains)
1Every row is removed; the table definition survives.
2TRUNCATE TABLE employees achieves the same faster, without row logging.
3DROP TABLE would remove the structure too — three different verbs.

Soft delete instead

Production often never really deletes.

query.sql
1UPDATE employees
2SET deleted_at = NOW()
3WHERE id = 4;
4-- readers add: WHERE deleted_at IS NULL
Result
idnamedepartmentsalaryage
1AliceEngineering8500029
2BobSales5200035
3CynthiaEngineering9100041
5ElenaSales5800026
6FaridEngineering7300033
1The row stays, flagged with a deletion timestamp.
2Every reader must remember the deleted_at IS NULL filter — usually via a view.
3Audit trails and "undo" become trivial; the trade is storage and query discipline.
--key facts
HierarchyDROP removes structure · TRUNCATE empties fast · DELETE filters rows
Undoinside a transaction ROLLBACK brings deleted rows back
Auditproduction deletes often soft-delete instead: SET deleted_at = NOW()
--use cases
Purging expired sessions, tokens and cache rows
Removing test data before shipping
GDPR erasure requests — genuinely removing a person's rows
Understanding soft-delete patterns that dominate production systems
--under the hood
DELETE logs every removed row (rollback-able, slower); TRUNCATE deallocates pages instead (fast, minimal undo).
Big purges go in batches (DELETE … LIMIT n in a loop) to keep transactions and replication lag small.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.