wasAsked.

What is the difference between DELETE, TRUNCATE and DROP in SQL?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · SQL

Community answers

Posted by wasAsked editorial collection

DELETE, TRUNCATE and DROP all remove data, but at different levels and with different behaviour. DELETE - A DML (data manipulation) command that removes rows from a table. - Can use a WHERE clause to remove specific rows: DELETE FROM Orders WHERE Status = 'Cancelled'; - Without WHERE, removes all rows but keeps the table structure. - Logs each deleted row in the transaction log, so it is slower for large tables. - Fires DELETE triggers. - Can be rolled back inside a transaction. - Respects foreign key constraints: deleting a referenced row fails unless cascading rules are defined. - Does not reset an IDENTITY column. TRUNCATE TABLE - Removes all rows from a table quickly; no WHERE clause. TRUNCATE TABLE StagingOrders; - Classified as DDL in SQL Server. It deallocates data pages rather than deleting row by row, so it is minimally logged and much faster on large tables. - Resets the IDENTITY seed to its original value. - Does not fire DELETE triggers. - Cannot be used on tables referenced by a foreign key (even if the referencing table is empty), or tables in indexed views or used in replication. - In SQL Server, TRUNCATE can be rolled back if it runs inside an explicit transaction (a common interview misconception is that it cannot). In some other databases (for example Oracle and MySQL), TRUNCATE commits implicitly and cannot be rolled back. - Requires ALTER permission on the table (more than DELETE permission). - SQL Server 2016+ supports truncating specific partitions: TRUNCATE TABLE Sales WITH (PARTITIONS (1 TO 3)); DROP TABLE - A DDL command that removes the entire table: data, structure, indexes, constraints, triggers and permissions. DROP TABLE OldOrders; - The table no longer exists afterwards. Dependent objects such as views may become invalid. - Fails if other tables reference it through foreign keys (drop those constraints first). - Can be rolled back in a SQL Server transaction, but generally treat it as permanent. - DROP TABLE IF EXISTS (SQL Server 2016+) avoids errors when the table does not exist. Comparison - What is removed: DELETE removes some or all rows; TRUNCATE removes all rows; DROP removes the table entirely. - WHERE clause: DELETE yes; TRUNCATE no; DROP not applicable. - Speed on large tables: DELETE slow; TRUNCATE fast; DROP fast. - Logging: DELETE logs every row; TRUNCATE logs page deallocations. - Triggers: DELETE fires them; TRUNCATE and DROP do not. - Identity reset: DELETE no; TRUNCATE yes; DROP not applicable. - Table structure after: DELETE and TRUNCATE keep it; DROP removes it. Practical guidance - Use DELETE for selective removal, or when triggers, foreign keys and audit logic must run. For very large deletes, delete in batches to avoid long locks and huge transaction logs. - Use TRUNCATE to quickly clear staging or temporary tables. - Use DROP when the table is no longer needed (usually through migration scripts), after taking backups.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics