SQL Lesson 15 - Deleting rows

When you need to delete data from a table in the database, you can use a DELETE statement, which describes the table to act on, and the rows of the table to delete through the WHERE clause.

Delete statement with condition

DELETE FROM mytable WHERE condition;

If you decide to leave out the WHERE constraint, then all rows are removed, which is a quick and easy way to clear out a table completely (if intentional).

Taking extra care

Like the UPDATE statement from last lesson, it's recommended that you run the constraint in a SELECT query first to ensure that you are removing the right rows. Without a proper backup or test database, it is downright easy to irrevocably remove data, so always read your DELETE statements twice and execute once.

Exercise

The database needs to be cleaned up a little bit, so try and delete a few rows in the tasks below.

1. This database is getting too big, lets remove all movies that were released **before** 2005.
	1. DELETE FROM movies WHERE Year < 2005;
2. Andrew Stanton has also left the studio, so please remove all movies directed by him.
	1. DELETE FROM movies WHERE Director = 'Andrew Stanton';

Exercise 15 — Tasks

1. This database is getting too big, lets remove all movies that were released **before** 2005. ✓
2. Andrew Stanton has also left the studio, so please remove all movies directed by him. ✓

The webpage is a lesson from SQLBolt on how to delete rows from a database using SQL. It covers:

The page strongly emphasizes double-checking your DELETE statements before executing them, since deleted data may be lost without a backup.

1