5) SQL Commands for Querying Databases Lesson

DELETE

3 min to complete · By Ryan Desmond

The SQL DELETE statement removes existing data from the database.

How Does SQL Delete Work?

When it is time for you to DELETE a record from one of your tables, you first need to choose the table that contains the data. Then, you apply a boolean condition to specify exactly what data to remove.

SQL Delete Statement Examples

Below is a good and a bad example of deleting data in SQL.

1. Safe Way to Delete Data

The safest way to delete data in a database is to apply a specific WHERE condition after the SQL DELETE statement.

For instance, if you remove the "Bryan Besmond" record with the actor_id of 201:

DELETE FROM sakila.actor
WHERE actor_id = 201; 

In the image below, you can tell this query was successful by the number of rows affected that it prints out under "Response".

Delete Bryan Besmond from Sakila.Actor table

2. Dangerous Way to Delete Data

If you do not indicate a WHERE clause, you will delete ALL RECORDS from the table! For instance, the following query will delete EVERY actor from the actor table - and remember, there is no "undo" in SQL:

delete from actor; -- bad

It looks harmless enough, but that's a powerful query that will wipe out all the data in the table. So be careful!

Summary: SQL Delete Statement

The SQL DELETE statement is used to delete data from your database. But remember: if you don't apply a WHERE condition, then you will delete every row from your table!

SQL Delete Syntax

Here's the syntax for the SQL DELETE statement, where you can substitute the variables in < > with your values.

DELETE FROM <YOUR_TABLE>
WHERE <YOUR_CONDITION>;

Next, you'll see a video summarizing how you update and delete information in your database.