Interview's MateSQL Learning
Interview's Mate

SQL Learning

Foundations → production SQL

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
SQL topics
Lesson 13/ 160
01. SQL Foundations & Relational Concepts

Deleting Data (DELETE vs TRUNCATE vs DROP)

Choose DELETE, TRUNCATE, or DROP based on recovery needs, speed, and schema impact.

BeginnerDELETE · TRUNCATE3 quiz questions
Practice SQL
In plain wordsDiagramExamples & theoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

DELETE removes some or all rows, TRUNCATE removes all rows very fast, DROP removes the table completely.

Think of it like…

DELETE is taking files out of a cabinet one by one and logging each; TRUNCATE is emptying the whole cabinet in one go; DROP is throwing the cabinet out.

Where you’ve seen it

Clearing a 50-million-row staging table every night: DELETE FROM staging logs 50 million row deletions and, in PostgreSQL, leaves dead rows for VACUUM; TRUNCATE staging finishes in milliseconds and frees the space immediately.

Words you’ll hear

DML vs DDL
DELETE is DML (changes data); TRUNCATE and DROP are DDL (change storage or structure).
Rollback
Undoing changes made inside an open transaction.
Identity reset
TRUNCATE … RESTART IDENTITY resets serial counters in PostgreSQL; DELETE never does.
Lock
TRUNCATE and DROP take an exclusive table lock; DELETE locks the rows it touches.

Visual diagram

Loading interactive diagram

Theory & example queries

What each command actually does

DELETE finds the matching rows and removes them one at a time, writing each change to the transaction log and firing row-level triggers. In PostgreSQL the old row versions stay on disk until VACUUM cleans them up, so a big DELETE doesn't shrink the table right away.

TRUNCATE skips the row-by-row work and releases the table's data pages in one step, which is why it takes milliseconds regardless of size. DROP removes the table definition as well, along with its indexes, constraints, triggers and grants. Views and code that use the table break.

Same goal, very different costQuery example
1Input dataStart from the source rows or state that the statement sees.
events before
3 rows
events before sample rows
idevent_type
1login
2click
3logout

Three representative rows are shown; the lesson discusses a 50-million-row table.

events after DELETE or TRUNCATE
0 rows
events after DELETE or TRUNCATE sample rows
idevent_type
No rows

Both commands leave the table empty. DELETE processes rows; TRUNCATE deallocates whole table data.

2QuerySwitch dialects, then select a line or execution step to trace it.
query.sql
1DELETE FROM events;
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Choose the target

Identify the table or database object the statement will modify.

1/1
query.sql
1TRUNCATE events;
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Choose the target

Identify the table or database object the statement will modify.

1/1
3OutputInspect the representative rows, affected-row count, plan, or command status.
Expected output
DELETE 50000000 TRUNCATE TABLE

Note: Statuses for the stated 50-million-row sample; command duration, locking and WAL volume are workload-dependent.

4ExplanationConnect the result back to the SQL rule and the edge case it demonstrates.

on 50 million rows can run for minutes and bloat the table;

finishes almost instantly.

Key takeaway: DELETE is selective and row-by-row; TRUNCATE is all-or-nothing and fast.

Rollback, triggers and foreign keys

DELETE can always be rolled back inside a transaction. TRUNCATE can be rolled back in PostgreSQL and SQL Server when it runs inside BEGIN … ROLLBACK, but MySQL and Oracle commit it implicitly, so there's no undo.

You usually can't TRUNCATE a table that other tables reference with foreign keys. PostgreSQL requires TRUNCATE … CASCADE, which also empties the referencing tables. For identity columns, PostgreSQL keeps counting unless you add RESTART IDENTITY, while MySQL and SQL Server reset the counter.

Deleting millions of old rows from a live tableQuery example
1Input dataStart from the source rows or state that the statement sees.
logs before batch
3 rows
logs before batch sample rows
idcreated_at
7012026-05-01
7022026-08-18
7032026-11-20
logs after batch
2 rows
logs after batch sample rows
idcreated_at
7022026-08-18
7032026-11-20

The old May row is removed while recent rows remain.

2QuerySwitch dialects, then select a line or execution step to trace it.
query.sql
1DELETE FROM logs2WHERE3  id IN (4    SELECT5      id6    FROM7      logs8    WHERE9      created_at < now() - interval '90 days'10    LIMIT11      1000012  );
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Choose the target

Identify the table or database object the statement will modify.

1/12
3OutputInspect the representative rows, affected-row count, plan, or command status.
Expected output
DELETE 10000 ... DELETE 0

Note: Representative sequence: full batches continue until a final pass finds no eligible rows; real batch counts depend on data volume.

4ExplanationConnect the result back to the SQL rule and the edge case it demonstrates.

Delete in batches,

, repeated until it affects 0 rows. Locks and log volume stay small.

Key takeaway: "Can I roll it back?" depends on both the command and the database.

Interactive walkthrough

How it works, step by step

Follow one practical SQL workflow from start to finish. Select any step or play through the flow.

Progress1/4
Step 1Decision

Do you need a WHERE?

If you're removing only some rows, DELETE is the only option. TRUNCATE and DROP always affect the whole table.

SQL at this step
1DELETE FROM orders2WHERE3  status = 'test'
What to watch at this step
Selective
DELETE only
Tip: use ← → to move between steps

When to use it & trade-offs

What it is

DELETE removes rows (all of them, or only those matching a WHERE) one at a time and fires triggers. TRUNCATE empties the whole table in one operation by releasing its storage. DROP removes the table itself: structure, data, indexes, constraints and permissions.

Why it matters: Picking the wrong one is how people lose production data or lock a busy table for minutes. It's also a top interview question because the differences (WHERE support, speed, rollback, triggers, identity reset) touch several core concepts at once.

When to reach for it

  • DELETE for retention jobs and removing specific bad records
  • TRUNCATE for nightly reloads of staging tables
  • DROP when retiring a table after a migration

When to avoid or be careful

  • DELETE without WHERE on a huge table when TRUNCATE would do
  • TRUNCATE … CASCADE without checking which tables it will empty
  • DROP in production without a verified backup

DELETE vs TRUNCATE vs DROP

DELETE
Best for: Removing some rows, or all rows when triggers and auditing must run

Strengths: WHERE clause, fires triggers, always rollback-able.

Watch out: Slow on big tables, logs every row, leaves dead rows in PostgreSQL.

TRUNCATE
Best for: Emptying staging or temporary tables

Strengths: Near-instant, frees space immediately, can reset identity.

Watch out: No WHERE, no row triggers, blocked by foreign keys, no rollback in MySQL or Oracle.

DROP
Best for: Removing a table you no longer need

Strengths: Removes data, structure, indexes and grants in one step.

Watch out: Irreversible without a backup; breaks views and code that use the table.

Interview signals & numbers to know

When this topic comes up

  • “DELETE vs TRUNCATE vs DROP”
  • “Can you roll back a TRUNCATE?”
  • Emptying a large table quickly
  • Resetting an auto-increment id
  • Audit triggers

Benchmarks & rules of thumb

DELETE -
DML: always rollback-able
TRUNCATE -
DDL: rollback-able in PostgreSQL and SQL Server only
DROP -
DDL: the table is gone

Common mistakes & follow-ups

Common interview mistakes

  • Saying "TRUNCATE can't be rolled back" as a universal rule. It can in PostgreSQL and SQL Server.
  • Running one giant DELETE on a busy table and locking it for minutes.
  • Forgetting that TRUNCATE doesn't fire DELETE triggers, so audit logs never see those rows.

Likely interviewer follow-ups

  • Why is TRUNCATE so much faster than DELETE?
  • Can you TRUNCATE a table that other tables reference with foreign keys?
  • How would you delete 100 million old rows from a live table without downtime?
Interview tip: Compare them on five points (WHERE support, speed, rollback, triggers, identity reset) and add the database-specific rollback nuance. That nuance usually earns the extra credit.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Deleting Data (DELETE vs TRUNCATE vs DROP) with hands-on SQL practice and interview Q&A.

Open SQL Practice →SQL interview Q&A
Previous topic
Updating Records Safely (UPDATE with WHERE)
Next topic
Basic Retrieval (SELECT, Column Aliases AS, DISTINCT)