sql
  1. sql-delete-all-rows

SQL DELETE All Rows

Syntax

To delete all rows from a table in SQL, use the DELETE statement with the FROM and WHERE clauses. The basic syntax for deleting all rows in a table is as follows:

DELETE FROM table_name WHERE condition;
  • table_name: the name of the table from which to delete all rows.
  • condition: the condition that must be met for a row to be deleted. Since we want to delete all rows, we can omit this condition.

Example

Suppose we have a table called customers with the following data:

id name email
1 John john@example.com
2 Jane jane@example.com
3 Bob bob@example.com

To delete all rows from this table, we can use the following SQL statement:

DELETE FROM customers;

Output

After executing the above SQL statement, all rows from the customers table will be deleted. The table will be empty.

Explanation

The DELETE statement is used to delete one or more rows from a table. When used without a WHERE clause, it deletes all rows from the table. The FROM clause specifies the name of the table from which to delete all rows. The WHERE clause is optional, but it can be used to specify a condition that must be met for a row to be deleted.

Use

The DELETE statement with the FROM and WHERE clauses can be used to delete all rows from a table. This can be useful when you want to start fresh with a table that has become cluttered with old or irrelevant data.

Important Points

  • The DELETE statement deletes one or more rows from a table.
  • When used without a WHERE clause, it deletes all rows from the table.
  • The FROM clause specifies the name of the table from which to delete rows.
  • The WHERE clause is optional, but it can be used to specify a condition that must be met for a row to be deleted.

Summary

In this tutorial, we learned how to delete all rows from a table in SQL using the DELETE statement with the FROM and WHERE clauses. We also saw an example of how to use this statement, along with its output and explanation. Finally, we discussed the use cases for deleting all rows and important points to keep in mind when using the DELETE statement.

Published on: