SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL Data Types SQL OPERATORS SQL COMMANDS SQL Queries What are single row and multiple row subqueries?

SQL Database

SQL Create Database SQL DROP Database SQL SELECT Database

SQL Table

SQL TABLE SQL CREATE TABLE SQL COPY TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL DROP TABLE SQL UPDATE TABLE SQL INSERT TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT WHERE Clause SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT OR Operator SQL SELECT LIKE Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL WHERE Clause SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT Statement SQL INSERT INTO Statement SQL INSERT INTO Values SQL INSERT INTO SELECT SQL Insert multiple rows

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL CROSS Join

SQL OPERATOR

SQL Comparison SQL LOGICAL Operator SQL Cast Operator SQL Arithmetic

Difference

SQL vs NOSQL WHERE vs HAVING DELETE vs DROP GROUP BY vs ORDER BY DROP vs TRUNCATE SQL IN vs SQL EXISTS Difference between Delete, Drop and Truncate in SQL

MISC

SQL SubQuery SQL CASE Commit and Rollback in SQL Pattern Matching in SQL DDL Commands in SQL DML Commands in SQL Types of SQL Commands SQL COUNT SQL Primary Key SQL FOREIGN KEY SET Operators in SQL Check Constraint in SQL SQL EXCEPT SQL VIEW SQL WHERE Statement SQL CRUD Operation Where Condition in SQL TCL Commands in SQL Types of SQL JOINS SQL Nth Highest Salary SQL NOT OPERATOR SQL UNION ALL SQL INTERSECT SQL Data Definition Language SQL Data Manipulation Language SQL Data Control Language SQL CONSTRAINTS SQL Aggregate Operators SQL KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL SQL Formatter WEB SQL SQL Auto Increment Save Point in SQL space() function in SQL SQL Aggregate Functions SQL Topological Sorting SQL Injection SQL Cloning Tables SQL Aliases SQL Handling Duplicate Update Query in SQL Grant Command in SQL SQL SET Keyword SQL Order BY LIMIT SQL Order BY RANDOM

How To

How to use the BETWEEN operator in SQL How To Use INNER JOIN In SQL How to use LIKE in SQL How to use HAVING Clause in SQL How to use GROUP BY Clause in SQL How To Remove Duplicates In SQL How To Delete A Row In SQL How to add column in table in SQL ? How to drop a column in SQL? How to create a database in SQL? How to use COUNT in SQL? How to Create Temporary Table in SQL? How to Add Foreign Key in SQL? How to Add Comments in SQL? How To Use Group By Clause In SQL How To Use Having Clause In SQL How To Delete Column In Table How To Compare Date In SQL How index works in SQL How to calculate age from Date of Birth in SQL How to Rename Column name in SQL How to get current year in SQL server 2012? User Input in PL/SQL

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.