A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

DELETE Statements: Removing Rows with Conditions

College Depth 72 in the knowledge graph ☐ I know this ☆ Set as goal
2topics build on this
333prerequisites beneath it
See this on the map →
SQL: INSERT, UPDATE, and DELETE (DML)SQL: WHERE Clause and Filtering→→DISTINCT: Eliminating Duplicate RowsUPDATE with JOINs: Conditional Updates
sql dml data-integrity

Core Idea

DELETE removes rows from a table based on WHERE conditions. Cascading deletes via foreign key constraints enforce referential integrity but require careful planning.

Explainer

From your work with INSERT and UPDATE, you know that SQL's data manipulation language (DML) modifies table contents. DELETE is the third major DML operation — it removes rows from a table. The basic syntax is straightforward: `DELETE FROM customers WHERE status = 'inactive'` removes every row matching the condition. The WHERE clause works exactly as it does in SELECT — you can use comparisons, logical operators, subqueries, and even joins to identify which rows to remove. This is where your understanding of filtering conditions becomes essential, because the WHERE clause is the only thing standing between a targeted cleanup and accidentally emptying an entire table.

A DELETE without a WHERE clause — `DELETE FROM customers` — removes every row in the table. This is almost never what you want, and it's one of the most dangerous statements in SQL. In practice, you should always write and test your WHERE clause as a SELECT first: run `SELECT * FROM customers WHERE status = 'inactive'` to see exactly which rows will be affected, verify the count and contents, and only then change SELECT to DELETE. This habit prevents catastrophic data loss.

The complexity of DELETE increases when tables have foreign key relationships. If you try to delete a customer who has orders in another table, the database faces a referential integrity conflict — those orders reference a customer that would no longer exist. The foreign key constraint's ON DELETE action determines what happens: `RESTRICT` (the default in most databases) blocks the delete entirely, `CASCADE` automatically deletes the dependent rows too, `SET NULL` sets the foreign key column to NULL, and `SET DEFAULT` resets it to its default value. Cascading deletes are powerful but dangerous — deleting one parent row might cascade through multiple levels of related tables, removing far more data than you intended. Always trace the cascade path before relying on it.

For large-scale deletions, performance matters. Deleting millions of rows in a single transaction locks the table, fills the transaction log, and can block other operations for extended periods. A common technique is batched deletion: delete in chunks of 1,000 or 10,000 rows at a time using a loop with `DELETE FROM table WHERE condition LIMIT 10000`, committing between batches. This keeps lock durations short and allows other transactions to interleave. Some databases also offer `TRUNCATE TABLE` as a faster alternative when you want to remove all rows — it deallocates entire data pages rather than logging individual row deletions, but it cannot be filtered with a WHERE clause and may not fire triggers.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Set Operations: Union, Intersection, and Complement → Relational Algebra → SQL: SELECT Statement and Basic Queries → SQL: WHERE Clause and Filtering → DELETE Statements: Removing Rows with Conditions

Longest path: 73 steps · 333 total prerequisite topics

Prerequisites (2)

Leads To (2)