Skip to main content
Beginnercomparison

What is the difference between DELETE, TRUNCATE and DROP in SQL?

ReportedSource: GeeksforGeeks interview experience: Cognizant GenC 2024

Quick answer

DELETE: DML, removes selected rows, logged, rollback-able, fires triggers. TRUNCATE: DDL, removes all rows fast, resets identity, usually auto-commits. DROP: removes the table definition and data entirely.

DELETE (DML).

  • Removes rows, optionally with WHERE.
  • Logs each row; can be rolled back inside a transaction; fires row-level triggers.
  • Slower on large tables; identity/auto-increment counters usually continue.
query.sql
DELETE FROM orders WHERE created_at < '2024-01-01';

TRUNCATE (DDL).

  • Removes all rows; no WHERE.
  • Deallocates data pages instead of logging each row, so it is very fast; resets identity counters in most databases.
  • Usually auto-commits (cannot be rolled back in Oracle/MySQL; can in PostgreSQL and SQL Server inside a transaction). Does not fire DELETE triggers; blocked if referenced by foreign keys (unless cascading).

DROP (DDL).

  • Removes the table itself: structure, data, indexes, constraints and permissions.

One-line summary. DELETE removes chosen rows, TRUNCATE empties the table, DROP removes the table.

See the concept: Join fan-out

Join fan-out
order_lines: one row per line
O101line 1₹300
payments: one row per attempt
O101P101FAILED
O101P102SUCCESS
lines JOIN payments ON order_id
line 1P101₹300
line 1P102₹300
SUM(line_revenue)₹600

Wrong: the single ₹300 line is counted once per payment attempt.

Fix: aggregate payments to one row per order first

line 1, paid, ₹300, so SUM = ₹300

One order line, two payment attempts: a direct join doubles the revenue. Aggregating payments to order grain first keeps one row per line.

Discussion