×

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 WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

4 minutes read.

How to use GROUP BY clause in SQL

In this SQL article, we will learn about the GROUP BY clause and how to use it in SQL. We will also discuss using the GROUP BY clause with the...

6 minutes read.

SQL CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

5 minutes read.

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 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 UNION ALL Operator

This page has all the information about UNION ALL operator, which is used to join all the values from the SELECT queries. Similar records were not considered in the result in...

3 minutes read.

SQL INTERSECT

SQL intersect operator is used to combine two or more SELECT statements, but it only displays the data similar to the SELECT statement. The syntax for the INTERSECT operation: SELECT COLUMN_NAME1, COLUMN_NAME2,...

3 minutes read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

3 minutes read.

SQL Count

Structured Query Language Count() Function is used with Structured Query Language SELECT Statement. SQL Count() function returns the number of items that match the specified criteria in the SELECT statement. Count()...

2 minutes read.

SQL Right Join

The SQL Right Join query displays all the table records and similar records from the left table. The query display zero records if it doesn’t find any similar records. If...

4 minutes read.

SQL SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language. What is the SET keyword? The SET keyword is used to specify values for the variables....

3 minutes read.

SQL ORDER BY

SQL ORDER BY The SQL ORDER BY clause is used to sort the data stored in tables in the database. The sorting can be done in an ascending way, descending way,...

5 minutes read.

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

6 minutes read.

SQL FULL JOIN

In this section, we will help you understand the concept of the SQL FULL Join clause with a few examples. The SQL FULL Join query is executed to display the integrated...

3 minutes read.

SQL GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is...

4 minutes read.

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

Trigger in SQL

In this article, we will learn about the concept of trigger in SQL and its implementation with the help of an example. A Trigger in Structured Query Language is a set...

4 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 Statement

The SQL SELECT statement is used to retrieve the data from the tables. We can also retrieve the selected records from the table using the query condition. The SQL SELECT...

5 minutes read.

Pattern Matching in SQL

The LIKE in the Structured Query Language is a Logical Operator. The SQL LIKE is used within the WHERE clause in the SQL. This Like Operator is used with the...

5 minutes read.