EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials SQL Tutorial SQL Clear Table
 

SQL Clear Table

Updated March 8, 2023

SQL Clear Table

 

 

Introduction to SQL Clear Table

We can clear the table contents in SQL using two ways. One of the ways of doing so is to use the data definition language command named TRUNCATE that deletes all the records present in the particular table that exists in our SQL database server. Another way of clearing the table is by using the Data manipulation language command DELETE FROM in which we can delete all the contents of the particular table by skipping any restriction on the columns of the table in the where clause and clearing the contents of the table. In this article, we will see how we can clear the table contents in SQL by using truncate command and delete command.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Using Truncate Command

We can use the ready-made method that is provided in the data definition language commands of SQL named TRUNCATE to remove all the contents of the table in SQL.

Syntax of Truncate Command:

TRUNCATE TABLE name of table;

In the above syntax, the name of the table is the table name of which we want to clear all the contents and records that exist in that table. The keyword TABLE needs to be used when using the TRUNCATE command.

Let us consider one example. The table named educba_identity1 which contains 5 rows and has the structure and contents as shown in the output of the below query statement.

Code:

SELECT * FROM `educba_identity1`;

Output:

SQL Clear Table 1

Now to delete all the rows and clear the table named educba_identity1 using the truncate command, we can make the use of the following query statement.

Code:

TRUNCATE TABLE `educba_identity1`;

Output:

SQL Clear Table 2

Let us once again check the contents of the table educba_identity1 and see whether the table is cleared using the following SQL command.

Code:

SELECT * FROM `educba_identity1`;

Output:

SQL Clear Table 3

We can observe that the table named educba_identity1 has been cleared successfully and there is none of the record present in the table.

Using Delete Command

We can clear the contents of the table by using the delete command which is one of SQL command of the set of the data manipulation language commands that are available in SQL. This command is most often used to delete the selected rows present in the table.

Syntax of Delete Command:

DELETE FROM name_of_table;

In the above syntax, the name of the table is the table name of which we want to clear all the contents and records that exist in that table. The keyword TABLE needs to be used when using the TRUNCATE command.

Let us consider one example. The table named educba_identity2 which contains 5 rows and has the structure and contents as shown in the output of the below query statement.

Code:

SELECT * FROM `educba_identity2`

Output:

SQL Clear Table 4

Now to delete all the rows and clear the table named educba_identity2 using the truncate command, we can make the use of the following query statement.

Code:

DELETE FROM `educba_identity2`;

Output:

SQL Clear Table 5

Let us once again check the contents of the table educba_identity1 and see whether the table is cleared using the following SQL command.

Code:

SELECT * FROM `educba_identity2`;

Output:

SQL Clear Table 6

We can observe that the table named educba_identity2 has been cleared successfully and there is none of the record present in the table.

Using Drop Table

We can alternatively use the drop command to delete the whole table along with its contents and structure that completely vanishes the existence of that table in our database in the SQL. But we will have to create the same table again using the CREATE table command in SQL. Note that drop command not only clears the table contents but also the structure of the table.

Let us consider one example in which we have one existing table named educba_writers_demo that has the columns and the contents of the table that are as shown in the output of the below query statement.

Code:

SELECT * FROM `educba_writers_demo`;

Output:

SQL Clear Table 7

Now, let us drop the table by using the drop command in SQL that clears the table contents as well as the table existence in our database.

Code:

DROP TABLE educba_writers_demo;

Output:

drop the table by using the drop command

Let us check the contents of the table by using the following query statement.

Code:

SELECT * FROM `educba_writers_demo`;

The execution of output of the above query statement is as shown below that shows the error saying that the table does not exist.

Output:

error saying

Let us recreate the table using the following create table command.

Code:

CREATE TABLE `educba_writers_demo` (
`id` INT(11) NOT NULL,
`expert_name` VARCHAR(10) NOT NULL,
`rate` DECIMAL(5,2) DEFAULT NULL,
`joining_date_time` DATETIME DEFAULT NULL
) ;

Output:

recreate the table

This successfully creates the table named educba_writers_demo that has the same structure. Let us try rechecking the contents of the table.

Code:

SELECT * FROM `educba_writers_demo`;

Output:

educba_writers_demo

This shows that the table contents have been cleared.

Conclusion – SQL Clear Table

We can clear the contents of the table in SQL by using multiple methods that include the usage of the data definition language command name truncates or data manipulation command named DELETE. An alternative method of clearing the contents of the table involves dropping the table that deleted the table as well as the contents of the table and recreates the table again using the CREATE table command.

Recommended Articles

We hope that this EDUCBA information on “SQL Clear Table” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. SQL TRUNCATE()
  2. SQL WITH AS Statement
  3. SQL CASE Statement
  4. SQL DML Commands

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW