Hook: Interviewers love DELETE because one missing WHERE can erase a whole table in a single breath.
Question: What does DELETE do in SQL?
Answer: DELETE removes rows from a table. It does not remove the table structure itself, and it is usually paired with a WHERE clause so you remove only the rows you intend. If you leave off WHERE, every row in the table is targeted.
Interview-Ready Answer: I use DELETE when I want to remove specific rows from a table. It is a DML statement, meaning it changes data rather than schema, and I almost always write a WHERE clause so I do not wipe the whole table by accident. In most databases it is transactional, so if I catch a mistake before COMMIT, I can roll it back.
DELETE MeansDELETE is a data change command: it removes rows, not the table itself. That makes it different from DROP, which removes the whole object, and from TRUNCATE, which empties the table much more aggressively.
WHERE clause can use an index, the engine can jump to the rows quickly; if not, it may scan the whole table.DELETE when you need to remove a small or medium set of rows with business rules, like expired sessions or canceled orders.WHERE clause lets you target exactly the right rows.| Command | Removes | WHERE? | Table stays? |
|---|---|---|---|
| DELETE | Selected rows | Yes | Yes |
| TRUNCATE | All rows | No | Yes |
| DROP | Table + data | No | No |
Memory rule: DELETE is the scalpel, TRUNCATE is the drain plug, and DROP is throwing away the whole sink.
If the filter matches k rows, the work is roughly proportional to those rows plus any index maintenance. With a good index, deleting 10 rows from a 10 million row table can be fast; without an index, the engine may scan all 10 million rows first, which is much slower. Large deletes can also create heavy log traffic and replication lag, so teams often delete in batches of 1,000 to 10,000 rows at a time.
WHERE clause: every row is targeted. This is legal, but dangerous.Real-World Story: Imagine a checkout service for an e-commerce site that stores temporary inventory reservations. A nightly cleanup job is supposed to remove only expired holds with DELETE so stock can be released back to shoppers. One developer accidentally ships DELETE FROM reservations; instead of filtering by expiration time. The job wipes every reservation, including active carts, and the next morning customers see items disappear from checkout or fail with mysterious stock errors.
What does the incident look like? Support tickets spike, logs show an unexpectedly large deleted-row count, and the database replication stream gets behind because one huge transaction floods the log. The team fixes it by restoring from backup or replaying from audit logs, then adds a safer pattern: verify the SELECT first, delete in small batches, and always review the WHERE clause before production execution.
DROP TABLE IF EXISTS inventory;
CREATE TABLE inventory (
product_id INTEGER PRIMARY KEY,
product_name VARCHAR(50) NOT NULL,
status VARCHAR(20) NOT NULL,
quantity INTEGER NOT NULL
);
INSERT INTO inventory (product_id, product_name, status, quantity) VALUES
(1, 'Keyboard', 'active', 25),
(2, 'Mouse', 'inactive', 0),
(3, 'Monitor', 'active', 10);
-- Verify the starting state so you can see exactly what gets removed.
SELECT *
FROM inventory
ORDER BY product_id;
-- Safe delete: remove only rows that match the rule.
DELETE FROM inventory
WHERE status = 'inactive';
SELECT *
FROM inventory
ORDER BY product_id;
-- Edge case: deleting a row that does not exist is not an error.
-- It simply affects 0 rows, which is useful to check in application code.
DELETE FROM inventory
WHERE product_id = 999;
SELECT *
FROM inventory
ORDER BY product_id;
-- Dangerous pattern to remember: without WHERE, every row is targeted.
-- DELETE FROM inventory;Follow-up & Tricky Questions:
DELETE and TRUNCATE? DELETE removes selected rows and can use WHERE; TRUNCATE removes all rows at once and usually skips row-by-row filtering. In many databases, TRUNCATE is faster, but it is less flexible.DELETE be rolled back? Usually yes if it happens inside an open transaction and you have not committed yet. That is one reason DELETE is safer than many people think, as long as transaction boundaries are respected.DELETE without WHERE? Every row in the table is a target. This is legal SQL, but it is the classic foot-gun, so interviewers want to hear that you would use it only when you truly intend to empty the table.DELETE affect foreign keys or triggers? Yes. A foreign key can block the delete or cascade it to child rows, and triggers can run extra logic like audit logging or cache cleanup.DELETE remove the table definition? No. It removes rows only; the columns, indexes, and other schema objects stay in place.DELETE always slow? No. Small targeted deletes can be fast, especially with an index on the filter column. It gets expensive when the database must scan many rows or update many indexes.Common Mistakes:
WHERE: the fix is to always write the filter first, then run a SELECT with the same condition before deleting.DELETE removes a table: it removes rows only; use DROP TABLE if you want the table object gone.Memory Hook: DELETE is the scissors: it cuts out rows you point at, but it does not tear up the whole folder. TRUNCATE empties the folder, and DROP throws the folder away.
Cheat Sheet:
DELETE removes rows, not the table.WHERE controls which rows are targeted.WHERE means all rows.Practice Tasks:
DELETE that removes all rows with status = 'inactive' from a sample table.DELETE, TRUNCATE, and DROP on a small test table.