SQL

How to Delete Table Data in SQL: A Complete Guide

intro

Today, we’re looking into ways we can purge unnecessary data from our databases. Let’s learn everything we can to effectively delete data in SQL!

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT

No matter if you’re a developer, DBA, or data analyst, chances are that you find yourself frequently working with data. And if you find yourself frequently working with data, you will sometimes inevitably need to delete some of it.

How to delete table data in SQL? That’s what we’re looking into today.

When to Delete Data Using SQL?

Before we talk about ways to delete table data in SQL, we need to understand when we should delete data using SQL. The most common scenarios are:

  • We no longer need the data for our specific use case.
  • The data is old and no longer applicable to our use case.
  • The data has been decommissioned and a specific time period has passed.

It’s worth noting that in many cases, “deleting” doesn’t mean deleting at all — in some applications, deleting means “marking” the data as deleted and perhaps moving it to another database/table so it’s no longer accessible by the application. This approach is called “soft delete.”

That’s why sometimes you may hear about applications that have deleted user data, but suffered a data breach and the data was still there.

How to Delete Data Using SQL?

Now that we know when we delete table data in SQL, we need to know how to do it too. Relational database management systems offer multiple ways to do so:

  1. Using the TRUNCATE statement
  2. Wiping fractions of data

A basic way to delete data using the DELETE FROM SQL statement looks like so:

Copy
        
1 DELETE FROM your_table 2 WHERE [boolean_expression];

Instead, a TRUNCATE statement looks like so:

Copy
        
1 TRUNCATE [table_name];

A DELETE FROM statement has a boolean expression after the WHERE clause that specifies exactly what data needs to be deleted. A TRUNCATE statement doesn’t support such an expression, as it deletes all data in a table in an instant. Because of that, it doesn’t come with as much overhead when compared to the DELETE FROM SQL query or when deleting fractions of data.

To wipe fractions of data, one can also create a table that only has the necessary columns when using the original table. e.g. a query like so would create a table (table_x) at the same time only bearing select columns from another table (table_y):

Copy
        
1 CREATE TABLE table_x AS SELECT columns FROM table_y;
Creating a table with select columns using DbVisualizer
Creating a table with select columns using DbVisualizer

In this case, we’ve created a table data_table2 with only two columns from data_table . Provided our data_table consists of more columns than that (it does — see below), some of the data is wiped without us even needing to employ a DELETE clause.

Columns within the Data Table in MySQL
Columns within the Data Table in MySQL

In other words, we’ve just wiped 4 columns out of the data set without even using a DELETE query. Smart, isn’t it? Learn more about creating tables using another table.

No matter which way to delete data you choose, always keep in mind the fact that different storage engines within your DBMS may approach the process of deleting data differently. For example, if the file_per_table option isn’t specified, MySQL’s InnoDB will only “mark” the data as deleted and still hold it inside of its tablespace (the ibdata1 file) because you didn’t tell the storage engine to store data in different files!

Some storage engines may not “link” the data to tablespaces (MySQL’s MyISAM is a great example of that), and in that regard, you may be able to just delete files instead of running SQL queries, but that depends on your use case since such storage engines don’t come with ACID capabilities and are often prone to crashes.

Another way to delete data pertains to partitioning. A query like so would drop a partition in demo_table, and with that, delete all of the within that partition:

Copy
        
1 ALTER TABLE demo_table DROP PARTITION [partitions]

Such an approach may be especially useful for those using partitioning by RANGE because when such a partitioning type is in use, it’s even easier to remove data after identifying it. Find out more about archiving data using partitions.

Summary

That’s it, really — delete table data in SQL isn’t rocket science, and basic measures do indeed go a long way. Before deleting data, evaluate your use case, think about the data you need to delete carefully, and employ measures wisely.

FAQ

What Is the Best Way to Delete Table Data in SQL?

There is no one “best” way to delete data in a table. Since you have multiple ways to delete data:

  1. Deleting data utilizing a WHERE clause — such a way to delete data employs a filter that tells our database what data we need to delete.
  2. Truncating tables as a whole — truncating a table removes all data in a table in the first place.
  3. Wiping out fractions of data — your database provides you with a way to wipe out fractions of data at speed (SELECT queries are often faster than DELETEs): use it!
  4. Using partitions — partitions can also be a great way to wipe out existing data in a table. Drop a partition and the data inside of it will be gone.

When to Delete Data in a Database?

Consider deleting data in a database once it’s no longer needed, once it’s old, or once it’s decommissioned. Alternatively, delete data to comply with regulations such as GDPR.

Dbvis download link img
About the author
LukasVileikisPhoto
Lukas Vileikis
Lukas Vileikis is an ethical hacker and a frequent conference speaker. He runs one of the biggest & fastest data breach search engines in the world - BreachDirectory.com, frequently speaks at conferences and blogs in multiple places including his blog over at lukasvileikis.com.
The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

How Often Should SQL Transaction Logs Be Backed Up?

author Antonello Zanini tags MySQL POSTGRESQL SQL SQL SERVER 7 min 2025-04-28
title

TRUNCATE vs DELETE: SQL Comparison of the Two Statements

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 9 min 2025-04-23
title

SQL Performance Tuning in Relational Databases: A Walkthrough

author Lukas Vileikis tags SQL 10 min 2025-04-22
title

How to Extract the Year from a Date in SQL

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 5 min 2025-04-21
title

A Complete Guide to an SQL Recursive Query Expression

author Lukas Vileikis tags MySQL SQL 5 min 2025-04-16
title

A Guide to the SQL Standard Deviation Functions

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-04-15
title

SQL EXISTS: Syntax and Use Cases with Examples

author Leslie S. Gyamfi tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-04-14
title

Welcoming Tables with Foreign Keys Into Your Database

author Lukas Vileikis tags MySQL SQL 5 min 2025-04-10
title

Dealing With NULL in SQL: Complete Guide

author Leslie S. Gyamfi tags SQL 7 min 2025-04-09
title

Data Backup and Recovery: Implement the Golden Rule in Your Database

author Lukas Vileikis tags Backup Recovery SQL 6 min 2025-04-07

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.