SQL DELETE statement

DELETE statement

To delete all the data from the table but not the table itself, use a query with a DELETE FROM statement.

For example, let's delete all the information from the following table titled books:

To do that with the query will be:

DELETE FROM books;

As a result of the query execution, the table books is empty now.

Delete selected rows

Use DELETE FROM statement to delete only selected rows from a table.

For example, let's delete some information from the following table titled books:

Some books from the table are unavailable (their quantity equals zero). To delete records about these books, the query with a WHERE clause will be:

DELETE FROM books 
WHERE quantity = 0;

This query will delete all the rows from the table books where the logical expression

quantity = 0

is true.

After the execution of the query, the books table will look like this:

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate

Master SQL by choosing your ideal learning course

View all courses