SQL Basics
Querying Data
Filtering Data
Joining Tables
SQL Functions
Modifying Data
Defining Data
SQL DELETE & TRUNCATE
In this tutorial, you will learn how to delete your rows from the database table using the SQL DELETE and TRUNCATE statements.
Syntax
The basic syntax of the DELETE statement to delete rows in a table, can be given with:
DELETE
FROM table_name
WHERE condition
The basic syntax of the TRUNCATE statement to delete all rows in a table, can be given with:
TRUNCATE TABLE table_name
Examples
To understand the DELETE and TRUNCATE statements in a better way, let’s look at the following customers and orders tables in our tutorial database:
Now, let’s check out some examples that demonstrate how it actually works.
We have the following task be to solve using SQL statements
The following Query will returns all the rows from customers table.
DELETE FROM customers
WHERE customer_id IN (6,7)
After executing the above query, you’ll get the result set something like this:
As you can see the output contains everything the whole customers tables including all rows and columns.
DELETE FROM customers
TRUNCATE customers
Let’s say we are only interested in getting only specific columns from a table, then we could use the following syntax of the SELECT statement:
The following Query will returns all the rows from customers table.
TRUNCATE TABLE customers
SELECT * helps you to examine the content of table that you are not familiar with. But be careful using it specially with big tables because database will retrieve everything which costs a lot of data movement across the network and might slow down the application