Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to SQL Notes
Topic #111

TRUNCATE TABLE in SQL

TRUNCATE TABLE removes all rows from a table instantly, but keeps the table structure intact — ready to insert into again.

Syntax

TRUNCATE TABLE table_name;

Example

TRUNCATE TABLE staging_orders;

Every row in staging_orders is gone; the table, its columns, indexes, and constraints remain.

TRUNCATE vs DELETE — The Classic Interview Comparison

TRUNCATEDELETE
RemovesAll rows onlyRows matching WHERE (or all, if no WHERE)
WHERE clause?Not allowedAllowed
Speed on large tablesMuch faster — deallocates data pagesSlower — removes row by row, logged individually
Auto-increment counterResets to start (in most databases)Keeps counting from where it left off
TriggersUsually does NOT fire DELETE triggersFires DELETE triggers
Transactional?PostgreSQL/SQL Server: yes. MySQL: implicit commit, harder to roll backFully transactional (COMMIT/ROLLBACK)

Practical Use Case

Clearing a staging table between nightly ETL loads, or resetting a test/demo table — anywhere you want "empty, like new" rather than selectively removing rows.

Common Mistakes

  • Trying to add a WHERE clause to TRUNCATE — it doesn't support one; use DELETE if you need conditional removal
  • Assuming TRUNCATE is always safely reversible with ROLLBACK — in MySQL, it behaves like DDL and commits immediately
  • Truncating a table that a foreign key still references — most databases will block this unless the constraint allows it

Interview Relevance

"Explain the difference between DELETE, TRUNCATE, and DROP" is one of the most frequently asked SQL basics questions. Structure your answer around: what's removed, whether WHERE is allowed, and transactional behavior — exactly the table above.

Practice Question

You need to remove only orders placed before 2023 from a table, not all of them. Should you use TRUNCATE or DELETE? Why?

Related SQL Notes

Want to go beyond the notes?

Join CodingNow's SQL course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →