×

Difference between Delete, Drop and Truncate in SQL

What is the Delete command in SQL?

In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL. We can say that Delete command is the part of Data Manipulation Language.

As we all know, the Delete command is used to delete records from the database table in SQL. With the help of the Delete command, we can delete the selected number of existing records from the database table in SQL.

We can also give a where clause commands to delete some specific rows or records from the database. The given condition decides which row should be deleted from the database. It helps us to maintain an entry in the transaction.

Syntax of Delete command in SQL:

DELETE FROM TableName WHERE condition;

Example:

Suppose we have a table named Employee.  Now, in this table, we have multiple columns but we want to remove the data only from the employee whose employee_Id is 1011. So, we will use the where clause statement according to the given condition.

DELETE FROM Employee WHERE Employee_id = 1011;

Now, if we run this command then the only row will be deleted because of the where clause condition given by the user. It doesn’t affect the entire table in SQL.

How does the Delete statement work?

Let’s take an employee table to understand how delete command works in detail.

Employee nameEmployee_id
Rohit1011
Yashraj1012
Shubham1023
Varun1021
Akash1022

Now if we execute the given condition below it will only delete only single record from the table employee.

DELETE FROM Employee WHERE Employee_id = 1011;

After the execution of the above condition, the row where employee_id is 1011 will be removed from the table and the table will be as follows:

Output:

Employee nameEmployee_id
Yashraj1012
Shubham1023
Varun1021
Akash1022

As we can see, the delete command changes the table according to the given condition.

What is the Truncate command in SQL?

The truncate command is the part of the Data Definition Language. Using this command allows us to modify the databases in SQL by truncating some entries.

As we all know, the truncate command is used to delete all the records from the database table in SQL. With the help of the truncate command, we can delete all the existing data and rows from the database table in SQL.

In the truncate command, we don’t need to give the condition to delete the database, we just need the name of the table. It resets the identity of the table.

It is very fast compared to the delete command because it does not need a condition like delete. It deletes all the rows and data from the table. It can not be rolled back after using.

Syntax of Truncate command in SQL:

TRUNCATE TABLE table_name;

Example:

Suppose, we have a table named Employee. In this employee table, we have multiple rows and columns and the value is stored in the form of rows. Apply the following command to truncate the table.

TRUNCATE TABLE Employee;

Now, if we run this command then only the column name will remain and all the data from the table will be deleted.

How does the Truncate command work?

Let’s take an employee table to understand how truncate command works in detail. Truncate command changes the identity of the table.

Employee nameEmployee_id
Rohit1011
Yashraj1012
Shubham1023
Varun1021
Akash1022

 Now, execute the following SQL query:

TRUNCATE TABLE Employee;

After the execution of this query, all the data from the table will be deleted except the structure of the table, and the table will not be deleted from the database but it will remain empty.

Employee nameEmployee_id

What is the Drop command in SQL?

Drop command is a DDL (Data Definition Language) command. We use the Drop command to modify the databases in SQL. With the help of the drop command, we can delete the entire table with all the data and also the structure of the table from the database in SQL.

Drop command is used to delete the entire existing table from the database. It can not be rolled back after we use this command.

Syntax of Drop command in SQL:

DROP TABLE table_name;

Example:

Suppose we have a table named Employee. In this employee table, we have multiple rows and columns, and the values are stored in the form of rows. Use the following SQL command to drop the table.

DROP TABLE table_name;

Now, if we execute the drop query, all the values from the table will be deleted with the structure of the table in the SQL database.


Related Topics

SQL Cast Operator

While writing SQL queries, we can see many values which are to be converted into another datatype for accessing them. In SQL this conversion is generally done by CAST function. CAST...

4 minutes read.

SQL Aggregate Functions

In this tutorial, we will learn and understand the SQL Aggregate Functions concept with the help of examples. SQL Aggregate Function is a function used to perform calculation operations on one...

4 minutes read.

SET Operators in SQL

The operator used to join or combine two queries is none other than SET operators. Operators categorized into SET operators are as follows: UNION Operator.UNION ALL’ Operator.INTERSECT Operator.MINUS Operator. Rules to be...

8 minutes read.

SQL Scalar Functions

TEACHER Table LCASE() Function The LCASE() function is used to return lower case values of specified column. Syntax: Select Lcase(column_name) from table_name; Example: Select Lcase(teacher_name) from teacher; Syntax: Select Lcase(column_name) from table_name [where condition]; Example: Select Lcase(teacher_name) from teacher where teacher_id=1; UCASE() Function The UCASE()...

1 minute read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

Difference between Delete, Drop and Truncate in SQL

What is the Delete command in SQL? In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL....

4 minutes read.

SQL Between Operator

SQL Between operator is a logical operator in the Structured Query Language. The Between operator is used to retrieve data within the range specified in the condition in the query. The...

7 minutes read.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 minutes read.

SQL Alter Table

In Structured Query Language, if you want to add columns in an existing table, then modify the table, or delete columns from the table. All these operations are allowed only...

7 minutes read.

SQL SELECT MAX

The SQL Max() function is an aggregate function in SQL. This function returns the values which are greater in the condition. The condition may be a number, or it may...

5 minutes read.

SQL COPY Table

In this tutorial, we will learn how to copy tables in SQL with the help of examples. Using the SELECT INTO statement in the SQL we can copy one table into...

4 minutes read.

SQL SELECT SUM

The SQL Sum() function is an aggregate function in SQL that returns the total values of an expression. The expression may be numerical, or it may be an expression. Syntax: SELECT SUM(columnname)...

3 minutes read.

DDL Commands in SQL

DDL stands for Data Definition Language. Data Definition Language is a bunch of SQL commands used to create, modify and delete Database schema but not the records or data. Data Definition...

5 minutes read.

How to use LIKE in SQL

In this SQL article, we will learn and understand how to use LIKE to the columns in the SQL tables. What is Like? Like is an operator in the SQL. It is...

7 minutes read.

SQL Top Clause

Top Clause: It returns the first ‘n’ number of rows as output. This Top clause is used to retrieve the required number of rows when there are thousands of records stored...

3 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

SQL WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

9 minutes read.

SQL SELECT BETWEEN Operator

This SQL tutorial explains and helps us understand how to use the BETWEEN operator in the SELECT query with examples. The SQL SELECT BETWEEN operator retrieves the data from the table...

2 minutes read.

SQL SELECT MIN

The SQL Min() function is an aggregate function in SQL. SQL Min() returns the minimum value of a given condition. The expression may be numerical, or it may be an expression. The syntax...

5 minutes read.