Explain the difference between DELETE, DROP, and TRUNCATE.

Beginner

Answer

  • DELETE: Removes specific rows based on WHERE clause, can be rolled back, triggers fire, slower
  • DROP: Removes entire table structure and data, cannot be rolled back, very fast
  • TRUNCATE: Removes all rows but keeps table structure, faster than DELETE, limited rollback
-- DELETE specific rows
DELETE FROM users WHERE age < 18;

-- TRUNCATE all data
TRUNCATE TABLE users;

-- DROP entire table
DROP TABLE users;