How do you delete all rows from a table?
To delete every row from a table, you can use either the DELETE or TRUNCATE commands.
1. Using DELETE
sql
DELETE FROM Table_Name;- Removes every record, but keeps the table's structure.
- The delete can be rolled back within a transaction (
ROLLBACK), if that's supported. - Can be slower on large tables, since it checks each record.
2. Using TRUNCATE
sql
TRUNCATE TABLE Table_Name;- Removes every record fast, also keeps the table's structure.
- Usually doesn't support a rollback in some DBMSs.
- Auto-increment counters are often reset.
Example:
sql
TRUNCATE TABLE Employees;Put simply,
DELETEandTRUNCATEboth clear a table entirely, butTRUNCATEdoes it faster and with no per-row checking.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.