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
| O101 | line 1 | ₹300 |
| O101 | P101 | FAILED |
| O101 | P102 | SUCCESS |
| line 1 | P101 | ₹300 |
| line 1 | P102 | ₹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