Full answer
SQLite has no TRUNCATE; a DELETE without WHERE uses an internal fast path when no triggers or foreign keys need row-by-row work:
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);sql
TRUNCATE TABLE order_items; -- error on purposetext
Error: near "TRUNCATE": syntax errorsql
DELETE FROM order_items;
SELECT COUNT(*) AS rows_left FROM order_items;| rows_left |
|---|
| 0 |
In PostgreSQL, TRUNCATE can be rolled back like other statements:
sql
BEGIN;
TRUNCATE order_items;
ROLLBACK;
SELECT COUNT(*) AS rows_after_rollback FROM order_items;| rows_after_rollback |
|---|
| 8 |
Points interviewers look for:
- TRUNCATE takes a stronger lock than DELETE.
- TRUNCATE refuses tables referenced by foreign keys unless CASCADE is given.
TRUNCATE ... RESTART IDENTITYresets identity sequences; DELETE does not.- DROP removes indexes, constraints and grants as well; recreating it needs the full DDL.
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
-- error on purpose
TRUNCATE TABLE order_items;
DELETE FROM order_items;
SELECT COUNT(*) AS rows_left FROM order_items;Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.
Key points
- DELETE removes rows and fires triggers
- TRUNCATE empties a table fast without row triggers
- TRUNCATE is transactional in PostgreSQL
- DROP removes the table definition
- SQLite has no TRUNCATE
Common mistakes
- Assuming TRUNCATE can never be rolled back
- Using DROP to empty a table and losing grants and indexes
Follow-ups
- Why does TRUNCATE need a stronger lock?
- What does RESTART IDENTITY do?
Tips
- Mention locks and triggers