SQL querying · Concept
DELETE, TRUNCATE and DROP
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voiceThe interviewer asks
What's the difference between DELETE, TRUNCATE and DROP? If you had to empty a 200-million-row log table in production, which would you use?
Then
Follow-ups the interviewer asks next
- Can you roll back a TRUNCATE?
- Do triggers fire for each one?
- What happens to the auto-increment counter?
- Why might deleting every row leave the table just as big on disk?
- Other tables have foreign keys to this one. What happens?
The answer
What a strong answer covers
- DELETE: removes rows one by one, takes a WHERE, fires row triggers, writes a log record per row, can be rolled back. PostgreSQL leaves dead rows that VACUUM later makes reusable; the file doesn't shrink. InnoDB keeps the space in the tablespace.
- TRUNCATE: removes all rows at once by replacing the table's storage. In PostgreSQL it is transactional (can be rolled back), takes an ACCESS EXCLUSIVE lock, keeps the identity counter unless you add
RESTART IDENTITY, and fails if other tables reference it unless you add CASCADE. In MySQL it is DDL: an implicit commit (no rollback), it resets AUTO_INCREMENT, and it fails if another table has a foreign key to it. - DROP removes the table itself: definition, indexes, grants.
- For the log table: TRUNCATE in a quiet moment if everything can go. If recent rows must stay, partition by time and drop old partitions. Never one huge DELETE: a long transaction, a flood of WAL or binlog, and replica lag.
The red pen
Common traps
Avoid:"TRUNCATE can never be rolled back" (true in MySQL, false in PostgreSQL); one giant DELETE in production.
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voicePractise questions like this out loud
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.