MySQL DELETE Query: How to Delete a Row from Table

โšก Smart Summary

MySQL DELETE Query removes rows that are no longer needed from a table and returns the number of rows affected. This resource explains DELETE syntax, WHERE filtering, multi-row deletion with IN, and the safer alternatives TRUNCATE and DROP.

  • ๐Ÿ—‘๏ธ Core Statement: DELETE FROM `table_name` [WHERE condition] removes whole rows and returns the count of rows deleted.
  • โš ๏ธ Omitted WHERE: Leaving out the WHERE clause deletes every row in the table, so always filter before executing.
  • ๐ŸŽฏ Targeted Removal: WHERE `movie_id` = 18 removes one record, while WHERE `movie_id` IN (20,21) removes several in a single statement.
  • ๐Ÿ” Irreversible Action: Deleted data cannot be recovered without a backup, therefore back up the database before running DELETE.
  • ๐Ÿ“Š Row-Level Scope: DELETE removes entire rows only; clearing a single column value requires UPDATE with NULL.
  • โš–๏ธ DELETE vs TRUNCATE vs DROP: DELETE filters rows, TRUNCATE empties a table instantly, and DROP removes the table structure itself.

What is the DELETE Query?

The MySQL DELETE statement is used to remove rows that are no longer required from a database table. It deletes the whole row from the table and returns the count of deleted rows. The DELETE statement comes in handy when you need to clear temporary or obsolete data from your database.

The DELETE query in MySQL can remove more than one row from a table in a single execution. This proves advantageous when you are removing large numbers of rows from a database table.

Once a row has been deleted, it cannot be recovered. Always make a database backup before deleting data, so the database can be restored and reviewed later if required.

How to Delete a Row in MySQL

With the purpose of the statement clear, the next step is the syntax. To delete a row in MySQL, the DELETE FROM statement is used:

DELETE FROM `table_name` [WHERE condition];

HERE:

  • DELETE FROM `table_name` tells the MySQL server to remove rows from the named table.
  • [WHERE condition] is optional and is used to apply a filter that restricts the number of rows affected by the DELETE query.

If the WHERE clause is not used in the MySQL DELETE query, then all the rows in a given table will be deleted.

โš ๏ธ Warning: Run the filter as a SELECT first. If it returns exactly the rows you intend to remove, the matching DELETE is safe. Full syntax is documented in the MySQL DELETE Statement manual.

Example of MySQL Delete Query

The next section builds a small data set and then deletes from it.

Step 1: Insert Sample Data

Before we go into a more detailed discussion about the SQL DELETE statement, let’s insert some sample data into the movies table to work with.

INSERT INTO `movies` (`title`, `director`, `year_released`, `category_id`) VALUES ('The Great Dictator', 'Charlie Chaplin', 1940, 7);
INSERT INTO `movies` (`title`, `director`, `category_id`) VALUES ('sample movie', 'Anonymous', 8);
INSERT INTO `movies` (`title`, `director`, `year_released`, `category_id`) VALUES ('movie 3', 'John Brown', 1920, 8);

Executing the above script adds three (3) movies into the movies table. Before going further, let’s list all the movies in our table.

SELECT * FROM `movies`;

Executing the above script gives us the following results. The three newly inserted rows are highlighted.

movie_id title director year_released category_id
1 Pirates of the Caribean 4 Rob Marshall 2011 1
2 Forgetting Sarah Marshal Nicholas Stoller 2008 2
3 X-Men NULL 2008 NULL
4 Code Name Black Edgar Jimz 2010 NULL
5 Daddy’s Little Girls NULL 2007 8
6 Angels and Demons NULL 2007 6
7 Davinci Code NULL 2007 6
9 Honey mooners John Schultz 2005 8
16 67% Guilty NULL 2012 NULL
18 The Great Dictator Charlie Chaplin 1940 7
19 sample movie Anonymous NULL 8
20 movie 3 John Brown 1920 8

Step 2: Delete a Single Row

Suppose the Myflix video library no longer wishes to rent out “The Great Dictator” and wants it removed from the database. Its movie id is 18, so the script below deletes that row.

DELETE FROM `movies` WHERE `movie_id` = 18;

Executing the above script in MySQL WorkBench against the Myflix database deletes the movie with id 18 from the table.

Let’s now check the current state of the movies table.

SELECT * FROM `movies`;
movie_id title director year_released category_id
1 Pirates of the Caribean 4 Rob Marshall 2011 1
2 Forgetting Sarah Marshal Nicholas Stoller 2008 2
3 X-Men NULL 2008 NULL
4 Code Name Black Edgar Jimz 2010 NULL
5 Daddy’s Little Girls NULL 2007 8
6 Angels and Demons NULL 2007 6
7 Davinci Code NULL 2007 6
9 Honey mooners John Schultz 2005 8
16 67% Guilty NULL 2012 NULL
19 sample movie Anonymous NULL 8
20 movie 3 John Brown 1920 8

NOTE:

  • The movie with id 18 is no longer returned in the query result set.
  • You cannot delete a single column from a table with DELETE. The statement removes an entire row. To clear one field, use an UPDATE query that sets the column to NULL.

Step 3: Delete Multiple Rows with IN

Let’s say we have a list of movies we want to delete. Rather than running one statement per row, we can combine the WHERE clause with the IN operator.

DELETE FROM `movies` WHERE `movie_id` IN (20,21);

Executing the above script deletes the movies with IDs 20 and 21 from our movies table in one pass.

DELETE vs TRUNCATE vs DROP in MySQL

DELETE is not the only way to remove data from MySQL. Beginners often reach for it when a faster command would fit better, so the table below compares the three removal commands.

Feature DELETE TRUNCATE DROP
Command type DML DDL DDL
WHERE clause Supported Not supported Not supported
What is removed Selected rows All rows Rows and table structure
Rollback Possible inside a transaction Not possible (implicit commit) Not possible (implicit commit)
AUTO_INCREMENT Counter is retained Counter resets to the start value Counter is removed with the table
Speed on large tables Slow (row by row) Very fast Very fast

Use DELETE when you need a filter or the ability to roll back. Use TRUNCATE TABLE when you want to empty a table quickly and reset its AUTO_INCREMENT counter. Use DROP only when the table itself is no longer needed, because it removes the definition along with the data.

FAQs

Only inside an open transaction. Run START TRANSACTION, execute the DELETE, then ROLLBACK to undo it. After COMMIT or with autocommit enabled, the rows are gone and only a backup can restore them.

Use a multi-table DELETE with a JOIN, naming both tables before the FROM keyword. Alternatively, define the foreign key with ON DELETE CASCADE so child rows are removed automatically when the parent row is deleted.

Safe Update Mode is enabled by default in MySQL Workbench. It rejects any DELETE whose WHERE clause does not use a key column, preventing accidental full-table deletions. Disable it under Edit > Preferences > SQL Editor.

Yes. AI assistants translate a plain-English request into DELETE syntax and can explain the WHERE clause. Always review the generated filter against a SELECT first, because an AI model cannot see your live data.

No. Let AI draft the statement, but execute it yourself on a backup or staging copy first. A missing WHERE clause deletes every row, and no AI tool can reverse that once the transaction is committed.

Summarize this post with: