We use DELETE and TRUNCATE to remove data from tables, but they work differently. Knowing how helps keep your data safe and your database fast.
DELETE vs TRUNCATE behavior in SQL
DELETE FROM table_name WHERE condition; TRUNCATE TABLE table_name;
DELETE removes rows one by one and can use a WHERE clause to filter rows.
TRUNCATE removes all rows quickly and cannot use a WHERE clause.
DELETE FROM employees WHERE department = 'Sales';
TRUNCATE TABLE employees;DELETE FROM orders;
This example first deletes the row where name is 'Bob', then shows remaining rows. Next, it truncates the table to remove all rows and shows that the table is empty.
CREATE TABLE test_table (id INT IDENTITY(1,1), name VARCHAR(20)); INSERT INTO test_table (name) VALUES ('Alice'), ('Bob'), ('Charlie'); -- Delete one row DELETE FROM test_table WHERE name = 'Bob'; SELECT * FROM test_table; -- Truncate table TRUNCATE TABLE test_table; SELECT * FROM test_table;
DELETE operations can be rolled back if inside a transaction; TRUNCATE usually cannot.
TRUNCATE resets identity counters (auto-increment numbers), DELETE does not.
TRUNCATE is faster for removing all rows because it does not log individual row deletions.
DELETE removes rows one by one and can filter with WHERE.
TRUNCATE removes all rows quickly without logging each row.
Use DELETE for selective removal and TRUNCATE for fast full table clearing.
Practice
DELETE and TRUNCATE in SQL?Solution
Step 1: Understand
DELETEbehaviorDELETEremoves rows individually and supports filtering withWHERE.Step 2: Understand
TRUNCATEbehaviorTRUNCATEremoves all rows quickly without logging each row, and does not supportWHERE.Final Answer:
DELETEremoves rows one by one and can use aWHEREclause;TRUNCATEremoves all rows quickly without logging each row. -> Option DQuick Check:
DELETE = selective row removal, TRUNCATE = fast full clear [OK]
- Thinking TRUNCATE supports WHERE clause
- Confusing which command logs each row
- Believing DELETE is faster than TRUNCATE
Employees using TRUNCATE?Solution
Step 1: Recall
The correct syntax to remove all rows quickly isTRUNCATEsyntaxTRUNCATE TABLE table_name;.Step 2: Check options for correctness
DELETE FROM Employees;uses DELETE;REMOVE ALL FROM Employees;is invalid SQL;TRUNCATE Employees WHERE id > 10;incorrectly adds WHERE to TRUNCATE;TRUNCATE TABLE Employees;matches the correct syntax.Final Answer:
TRUNCATE TABLE Employees; -> Option BQuick Check:
TRUNCATE syntax = TRUNCATE TABLE [OK]
- Adding WHERE clause to TRUNCATE
- Using DELETE syntax for TRUNCATE
- Using non-existent REMOVE command
Orders with 1000 rows, what will be the number of rows after executing DELETE FROM Orders WHERE OrderDate < '2023-01-01'; if 300 rows have OrderDate before 2023?Solution
Step 1: Understand DELETE with WHERE clause
The DELETE command removes only rows matching the condition: OrderDate < '2023-01-01'.Step 2: Calculate remaining rows
Since 300 rows match the condition, they are deleted. Remaining rows = 1000 - 300 = 700.Final Answer:
700 rows remain -> Option AQuick Check:
1000 - 300 = 700 rows left [OK]
- Assuming DELETE removes all rows without WHERE
- Confusing DELETE with TRUNCATE behavior
- Thinking syntax error due to date format
TRUNCATE TABLE Customers WHERE Country = 'USA'; but get an error. What is the most likely cause?Solution
Step 1: Check
TRUNCATEcommand rulesTRUNCATEremoves all rows and does not allow filtering withWHERE.Step 2: Identify error cause
UsingWHEREwithTRUNCATEcauses a syntax error becauseTRUNCATEdoes not supportWHEREclauses.Final Answer:
TRUNCATEdoes not supportWHEREclauses. -> Option AQuick Check:
TRUNCATE = no WHERE allowed [OK]
- Assuming TRUNCATE supports WHERE
- Thinking table absence causes this error
- Believing DELETE must always replace TRUNCATE
Logs but keep the table structure and reset identity counters. Which command should you use and why?Solution
Step 1: Identify command for fast full table clearing
TRUNCATE TABLEremoves all rows quickly and resets identity counters.Step 2: Compare with DELETE and DROP
DELETEremoves rows slowly and does not reset identity counters;DROPremoves table structure.Final Answer:
UseTRUNCATE TABLE Logs;because it removes all rows fast and resets identity counters. -> Option CQuick Check:
TRUNCATE = fast clear + reset identity [OK]
- Thinking DELETE resets identity counters
- Using DROP when structure must be kept
- Believing DELETE is faster than TRUNCATE
Ready to prove it?
Pass the test and this pattern moves unknown
