This is one of the common SQL interview questions. Moreover, these commands are used more often in our day to day work. So let us go ahead and learn the purpose of each command. Hopefully, this short article will be beneficial for you.
Before we understand the commands, there is an important concept which we need to understand first:
What is Data and Schema in SQL Table?
A table is created using the Create command and it specifies the structure / skeleton of the table. It specifies the name of the table, no. of columns a table will have, the data type of each column etc. Whereas the Data is the actual values which get inserted as a row for each of the table columns.
Let us understand the main question now:
Definition: Delete command is used to delete existing records / rows from the table.
Syntax: Delete From SchemaName.TableName where ColumnName = filterValue;
Definition: Truncate command is used to delete all the records / rows from the table. In other words, It empties the table.
Syntax: Truncate Table SchemaName.TableName;
Definition: Drop command is used to delete the table schema along with data.
Syntax: Drop Table SchemaName.TableName;
Delete and Truncate, which one to use when?