SQL CRUD Basics Part 4 – Delete.

In this final part of the SQL CRUD Basics series, we visit the all-mighty and powerful DELETE command. Does that word frighten you? It should, as DELETE will completely remove rows of that oh-so-important data from your database table. Without warning or question, it will be gone. Perhaps your goal is to remove all rows. Great, no problem. However, removing a specific row or set of rows – instead of them all – requires filtering with a WHERE clause predicate, just the same as you would in SELECT and UPDATE statements. Continue reading to see DELETE command examples for better understanding…

blurry-image-of-code-and-data-laptop-screen
Photo by Shahadat Shemul on Unsplash

Note: All data, names or naming found within the database presented in this post, are strictly used for practice, learning, instruction, and testing purposes. It by no means depicts actual data belonging to or being used by any party or organization.

OS and DB used:

  • OpenSuse Leap 15.1
  • MySQL 8.0.18


Self-Promotion:

If you enjoy the content written here, by all means, share this blog and your favorite post(s) with others who may benefit from or like it as well. Since coffee is my favorite drink, you can even buy me one if you would like!


SQL CRUD Basics Series

Be sure and visit the accompanying blog posts in the series so far:

Although MySQL’s DELETE syntax – along with other SQL dialects – is more complex than what I demonstrate here, the most basic syntax follows this pattern:

1
2
3
DELETE
FROM some_table
WHERE some_conditional; -- the WHERE clause is optional.

Here is the present data in the fictitious ‘friends’ table used throughout the series:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mysql> SELECT *
    -> FROM friends;
+------------+-----------+--------------+------------+------+
| first_name | last_name | phone_num    | birthday   | age  |
+------------+-----------+--------------+------------+------+
| Jim        | Dandy     | 476-111-1122 | 1977-07-19 |   42 |
| Tara       | Runner    | 777-767-9900 | 1980-01-23 |   39 |
| Max        | Maxer     | 398-392-5656 | 1975-01-23 |   44 |
| Mary       | Moore     | 212-543-9420 | 1978-03-23 |   41 |
| Charlie    | Charles   | 888-767-2323 | 1971-08-22 |   48 |
| Humpty     | Dumpty    | 118-257-7344 | 1971-11-22 |   48 |
| Roger      | Dodger    | 234-767-3983 | 1975-08-22 |   44 |
+------------+-----------+--------------+------------+------+
7 rows in set (0.00 sec)

No need to beat around the bush. I’ll dive right in head-first with DELETE.

In order to retain the data – yet provide an example of an unfiltered DELETE statement – I’ll operate within a TRANSACTION so I can ROLLBACK the changes:

1
2
3
4
5
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)

mysql> DELETE FROM friends;
Query OK, 7 rows affected (0.01 sec)
(Interested in TRANSACTIONS? I have a great post to get you started. Read MySQL Transactions and User-defined session variables. to learn about this safety net you can use.)

After that DELETE statement, what data is left in the ‘friends’ table?

1
2
3
mysql> SELECT *
    -> FROM friends;
Empty set (0.00 sec)

Nada. Zilch. Nothing. And that my friends is the power of DELETE. Had there been 100,000 rows of data in table ‘friends’, they would also be gone like ashes in the wind.

Let’s get that data back by calling the ROLLBACK command:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
mysql> ROLLBACK;
Query OK, 0 rows affected (0.25 sec)

mysql> SELECT *
    -> FROM friends;
+------------+-----------+--------------+------------+------+
| first_name | last_name | phone_num    | birthday   | age  |
+------------+-----------+--------------+------------+------+
| Jim        | Dandy     | 476-111-1122 | 1977-07-19 |   42 |
| Tara       | Runner    | 777-767-9900 | 1980-01-23 |   39 |
| Max        | Maxer     | 398-392-5656 | 1975-01-23 |   44 |
| Mary       | Moore     | 212-543-9420 | 1978-03-23 |   41 |
| Charlie    | Charles   | 888-767-2323 | 1971-08-22 |   48 |
| Humpty     | Dumpty    | 118-257-7344 | 1971-11-22 |   48 |
| Roger      | Dodger    | 234-767-3983 | 1975-08-22 |   44 |
+------------+-----------+--------------+------------+------+
7 rows in set (0.00 sec)

Imagine I only need to store a table of ‘friends’ who are 40 years old or older. I can easily apply a predicate in the WHERE clause and target that specific set of rows (if multiple rows match) for deletion. First, I will run an exploratory query and determine which row(s) meet the criteria:

1
2
3
4
5
6
7
8
9
mysql> SELECT *
    -> FROM friends
    -> WHERE age < 40;
+------------+-----------+--------------+------------+------+
| first_name | last_name | phone_num    | birthday   | age  |
+------------+-----------+--------------+------------+------+
| Tara       | Runner    | 777-767-9900 | 1980-01-23 |   39 |
+------------+-----------+--------------+------------+------+
1 row in set (0.00 sec)

Same as you saw in the example UPDATE statements from SQL CRUD Basics Part 3 – Update, just replace the SELECT portion of the syntax with the appropriate DELETE syntax:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
mysql> DELETE
    -> FROM friends
    -> WHERE age < 40;
Query OK, 1 row affected (0.22 sec)

mysql> SELECT *
    -> FROM friends;
+------------+-----------+--------------+------------+------+
| first_name | last_name | phone_num    | birthday   | age  |
+------------+-----------+--------------+------------+------+
| Jim        | Dandy     | 476-111-1122 | 1977-07-19 |   42 |
| Max        | Maxer     | 398-392-5656 | 1975-01-23 |   44 |
| Mary       | Moore     | 212-543-9420 | 1978-03-23 |   41 |
| Charlie    | Charles   | 888-767-2323 | 1971-08-22 |   48 |
| Humpty     | Dumpty    | 118-257-7344 | 1971-11-22 |   48 |
| Roger      | Dodger    | 234-767-3983 | 1975-08-22 |   44 |
+------------+-----------+--------------+------------+------+
6 rows in set (0.01 sec)

You can see from the SELECT query following the DELTE statement, the row with the ‘age’ column value of 39 (less than 40) is gone.

Like other DML commands (those that modify or remove data), DELETE requires attention to what row or set of rows are targeted. The most important point to remember is this: without a WHERE clause, DELETE will remove all rows from the named table.

Ultra congratulations to you! You have learned about each of the SQL CRUD operations and can now go forth and use those commands where applicable as needed. Thanks for sticking it out through the series!!!

I would cherish your valuable feedback in the comments section on the series as a whole. What did you like? What did you not like? What did I not include? What should I have left out? Would you be interested in all 4 posts of the series combined into 1 handy reference PDF? Please let me know your thoughts via comments below and many many thanks!!!

Thanks for reading!!!

Like what you have read? See anything incorrect? Please comment below and thanks for reading!!!

A Call To Action!

Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.

Visit the Portfolio-Projects page to see blog post/technical writing I have completed for clients.

Have I mentioned how much I love a cup of coffee?!?!

To receive email notifications (Never Spam) from this blog (“Digital Owl’s Prose”) for the latest blog posts as they are published, please subscribe (of your own volition) by clicking the ‘Click To Subscribe!’ button in the sidebar on the homepage! (Feel free at any time to review the Digital Owl’s Prose Privacy Policy Page for any questions you may have about: email updates, opt-in, opt-out, contact forms, etc…)

Be sure and visit the “Best Of” page for a collection of my best blog posts.


Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.

Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, is performed on a personal development/learning workstation-environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.

Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.