Post content
🧑💼Interviewer: What’s the difference between DELETE and TRUNCATE? 👨💻Me: Both commands are used to remove data from a table, but they work differently: 🔹DELETE – Removes rows one by one, based on a WHERE condition (optional). – Logs each row deletion, so it’s slower. – Can be rolled back if used within a transaction. – Triggers can fire on deletion. 🔹TRUNCATE – Removes all rows instantly—no WHERE clause allowed. – Faster, uses minimal logging. – Cannot delete specific rows—it's all or nothing. – Usually can’t be rolled back in some databases. 🧪Example: -- DELETE only inactive users DELETE FROM users WHERE status = 'inactive'; -- TRUNCATE entire users table TRUNCATE TABLE users; 💡Tip: Use DELETE when you need conditions. Use TRUNCATE for a quick full cleanup. 💬Tap ❤️ if this helped you!