📖 Scenario: You are managing a small company's employee database. Sometimes you need to remove all records from a table quickly, and other times you want to delete records one by one or selectively.
🎯 Goal: Build SQL commands to create a table, insert data, and then practice removing data using both DELETE and TRUNCATE commands to see how they behave differently.
📋 What You'll Learn
Create a table named employees with columns id (integer) and name (text).
Insert exactly three employees with ids 1, 2, and 3 and names 'Alice', 'Bob', and 'Charlie'.
Write a DELETE statement to remove the employee with id 2.
Write a TRUNCATE statement to remove all remaining employees quickly.
💡 Why This Matters
🌍 Real World
Database administrators and developers often need to remove data efficiently and safely. Knowing when to use DELETE or TRUNCATE helps maintain data integrity and performance.
💼 Career
Understanding DELETE vs TRUNCATE is essential for roles like database developer, data analyst, and backend engineer to manage data cleanup and maintenance tasks.
Progress0 / 4 steps
1
Create the employees table and insert data
Write SQL commands to create a table called employees with columns id as integer and name as text. Then insert three rows with these exact values: (1, 'Alice'), (2, 'Bob'), and (3, 'Charlie').
SQL
Hint
Use CREATE TABLE to make the table, then use three INSERT INTO statements for each employee.
2
Add a DELETE statement to remove one employee
Add a SQL DELETE statement that removes the employee with id equal to 2 from the employees table.
SQL
Hint
Use DELETE FROM employees WHERE id = 2; to remove the employee with id 2.
3
Add a TRUNCATE statement to remove all remaining employees
Add a SQL TRUNCATE statement that removes all remaining rows from the employees table quickly.
SQL
Hint
Use TRUNCATE TABLE employees; to quickly remove all rows.
4
Add a comment explaining the difference between DELETE and TRUNCATE
Add a SQL comment explaining that DELETE removes rows one by one and can be selective, while TRUNCATE removes all rows quickly and resets the table.
SQL
Hint
Use SQL comment syntax -- to add your explanation.
Practice
(1/5)
1. Which statement correctly describes the difference between DELETE and TRUNCATE in SQL?
easy
A. TRUNCATE is slower than DELETE because it logs each row removal.
B. TRUNCATE removes rows one by one and can use a WHERE clause; DELETE removes all rows quickly without logging each row.
C. DELETE and TRUNCATE both remove rows one by one and support WHERE clauses.
D. DELETE removes rows one by one and can use a WHERE clause; TRUNCATE removes all rows quickly without logging each row.
Solution
Step 1: Understand DELETE behavior
DELETE removes rows individually and supports filtering with WHERE.
Step 2: Understand TRUNCATE behavior
TRUNCATE removes all rows quickly without logging each row, and does not support WHERE.
Final Answer:
DELETE removes rows one by one and can use a WHERE clause; TRUNCATE removes all rows quickly without logging each row. -> Option D
Quick Check:
DELETE = selective row removal, TRUNCATE = fast full clear [OK]
Hint: DELETE filters rows; TRUNCATE clears all fast [OK]
Common Mistakes:
Thinking TRUNCATE supports WHERE clause
Confusing which command logs each row
Believing DELETE is faster than TRUNCATE
2. Which of the following is the correct syntax to remove all rows from a table named Employees using TRUNCATE?
easy
A. DELETE FROM Employees;
B. TRUNCATE TABLE Employees;
C. TRUNCATE Employees WHERE id > 10;
D. REMOVE ALL FROM Employees;
Solution
Step 1: Recall TRUNCATE syntax
The correct syntax to remove all rows quickly is TRUNCATE 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 B
Quick Check:
TRUNCATE syntax = TRUNCATE TABLE [OK]
Hint: TRUNCATE always uses TABLE keyword [OK]
Common Mistakes:
Adding WHERE clause to TRUNCATE
Using DELETE syntax for TRUNCATE
Using non-existent REMOVE command
3. Given a table 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?
medium
A. 700 rows remain
B. 0 rows remain
C. 1000 rows remain
D. Syntax error
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 A
Quick Check:
1000 - 300 = 700 rows left [OK]
Hint: DELETE with WHERE removes only matching rows [OK]
Common Mistakes:
Assuming DELETE removes all rows without WHERE
Confusing DELETE with TRUNCATE behavior
Thinking syntax error due to date format
4. You run the command TRUNCATE TABLE Customers WHERE Country = 'USA'; but get an error. What is the most likely cause?
medium
A. TRUNCATE does not support WHERE clauses.
B. The table Customers does not exist.
C. TRUNCATE requires a WHERE clause.
D. You must use DELETE instead of TRUNCATE for all tables.
Solution
Step 1: Check TRUNCATE command rules
TRUNCATE removes all rows and does not allow filtering with WHERE.
Step 2: Identify error cause
Using WHERE with TRUNCATE causes a syntax error because TRUNCATE does not support WHERE clauses.
Final Answer:
TRUNCATE does not support WHERE clauses. -> Option A
Quick Check:
TRUNCATE = no WHERE allowed [OK]
Hint: TRUNCATE never uses WHERE clause [OK]
Common Mistakes:
Assuming TRUNCATE supports WHERE
Thinking table absence causes this error
Believing DELETE must always replace TRUNCATE
5. You want to quickly remove all rows from a large table Logs but keep the table structure and reset identity counters. Which command should you use and why?
hard
A. Use DELETE FROM Logs WHERE 1=1; because it is faster than TRUNCATE.
B. Use DELETE FROM Logs; because it removes rows and resets identity counters.
C. Use TRUNCATE TABLE Logs; because it removes all rows fast and resets identity counters.
D. Use DROP TABLE Logs; to remove all rows and keep structure.
Solution
Step 1: Identify command for fast full table clearing
TRUNCATE TABLE removes all rows quickly and resets identity counters.
Step 2: Compare with DELETE and DROP
DELETE removes rows slowly and does not reset identity counters; DROP removes table structure.
Final Answer:
Use TRUNCATE TABLE Logs; because it removes all rows fast and resets identity counters. -> Option C
Quick Check:
TRUNCATE = fast clear + reset identity [OK]
Hint: TRUNCATE clears fast and resets identity counters [OK]
Common Mistakes:
Thinking DELETE resets identity counters
Using DROP when structure must be kept
Believing DELETE is faster than TRUNCATE
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.