SQL Delete Statement

SQL Delete Statement

This is a detailed tutorial of the SQL Delete Statement. Learn how you can delete one, multiple or all table records using the DELETE query in SQL.

Use of SQL Delete Statement

SQL Delete Statement is used to delete existing records of your database table. As I also mentioned in the SQL Update Statement article, you must also use the SQL WHERE clause in the Delete Statement as well. The WHERE clause in the delete statement also helps to identify the right table record to be deleted. If you do not use the WHERE clause, all of the table records will be deleted. With Delete statement, you can delete one or more table rows in a single query.

Delete Syntax

The following syntax will delete all the table rows where the condition is true for the table_name database table.

Again, WHERE clause is optional, but not using it will delete all the table records.

Delete Query Example

In this section, I’ve three different examples of the SQL Delete query. Each example illustrates a different case of deleting table records.

The first query deletes a single table row where the condition is True. The second one deletes multiple table rows where the condition so is written in WHERE clause is True. And in the third query, all of the table records are being deleted because the WHERE clause is not used in the query.

For demonstrating the examples, we have got a demo database in which we’ll run the queries for a table named issued_books.

The following screenshot shows the first 10 records of this database table with all column names and values.

SQL Delete Statement Demo Database Table

Deleting Single Record

Query 1.

The following query deletes the table row with ID 11.

Result.

SQL Delete Query Single Record Example

Deleting Multiple Records

Query 2.

The query written below will delete all the table rows where the value for status is RETURNED and the value for book_id is 7.

Result.

Delete Multiple Rows SQL Delete Statement Example

The output success message shows that there were two rows satisfying the condition that have been affected. Here the affected rows are the deleted rows.

Deleting All Records

Query 3.

The following DELETE query that is written without WHERE clause will delete all the records of the table.

Result.

Delete All Records SQL Delete Statement Example

As you can see in the output success message, there were 18 rows in the database table that has been affected or deleted.

I hope you find this guide useful. If so, do share it with others who are willing to learn SQL. If you have any questions related to this article, feel free to ask them in the comments section.

Helpful Associated Guides.

Also, don’t forget to Subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *