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.
The following syntax will delete all the table rows where the condition is true for the
table_name database table.
DELETE FROM table_name WHERE condition;
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
The following screenshot shows the first 10 records of this database table with all column names and values.
Deleting Single Record
The following query deletes the table row with ID 11.
DELETE from issued_books WHERE ID = 11
Deleting Multiple Records
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.
DELETE FROM issued_books WHERE status = 'RETURNED' AND book_id = 7
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
The following DELETE query that is written without WHERE clause will delete all the records of the table.
DELETE FROM issued_books
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!