The Wayback Machine - https://web.archive.org/web/20240624115852/https://www.geeksforgeeks.org/mysql-delete-statement/
Open In App

MySQL DELETE Statement

Last Updated : 14 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In DBMS, CRUD operations (Create, Read, Update, Delete) are essential for effective data management. The Delete operation is crucial for removing data from a database. This guide covers the MySQL DELETE statement, exploring its syntax and providing examples.

Understanding how DELETE works helps ensure accurate and safe data removal, preventing accidental loss and maintaining data integrity.

DELETE Statement

The MySQL DELETE statement deletes one or more existing records from a table. It is commonly used with the WHERE or JOIN clause. It is a Data Manipulation Language (DML) statement. Generally, you cannot ROLLBACK (undo) after performing the DELETE statement. You can delete the entire table data using DELETE or delete only specific rows.

Syntax

DELETE FROM table_name WHERE condition;

Note: Be careful when using the DELETE statement in MySQL. Always use a DELETE statement with a WHERE clause.

Demo MySQL Database

To create this table in your system write the following MySQL queries:

CREATE TABLE students (
ID int NOT NULL,
Name varchar(255) NOT NULL,
Department varchar(255) NOT NULL,
Location varchar(255) NOT NULL,
PRIMARY KEY (ID)
);

INSERT INTO students (ID, Name, Department, Location) VALUES
(12, 'Ravi', 'IT', 'Hyderabad'),
(15, 'Kiran', 'MECH', 'Mysore'),
(18, 'Navya', 'CSE', 'Hyderabad'),
(20, 'Rahul', 'CIVIL', 'Chennai'),
(22, 'Alex', 'ECE', 'Bengaluru'),
(24, 'Bob', 'IT', 'Vizag');

Output:

ID

Name

Department

Location

12

Ravi

IT

Hyderabad

15

Kiran

MECH

Mysore

18

Navya

CSE

Hyderabad

20

Rahul

CIVIL

Chennai

22

Alex

ECE

Bengaluru

24

Bob

IT

Vizag

DELETE Statement Examples

Let’s look at some MySQL DELETE statement examples to understand its working and also cover different use cases of DELETE statement.

Example 1: DELETE Statement with WHERE Clause

We can use DELETE statement with WHERE clause, to specifically delete some data from the table. In this example, we will delete the rows of students who belongs to IT or CSE and from Hyderabad.

Query:

DELETE FROM STUDENT 
WHERE (Department ='IT' or Department ='CSE') and location ='Hyderabad';

Output:

MySQL-DELETE--Example-With-WHERE-Condition

Output

Explanation: We have deleted Student details of Ravi and Navya as they are satisfying the conditions of location as ‘Hyderabad’ and Department of Ravi is ‘IT’ and Department of Navya is ‘CSE’.

Example 2: Delete the Entire Data From the Table

We can delete the entire data from the table, by not using the WHERE clause. Consider the same Student table above with 6 entries and columns id, name, department, and location columns.

Query:

DELETE FROM Student;

Output:

delete table example

DELETE statement without WHERE clause.

Explanation: Entries in the table have been deleted completely when we do not use the WHERE clause, As you can see in the output, there are no rows left.

MySQL DELETE With LIMIT Clause

In MySQL, Using DELETE with LIMIT will allow us to specify the maximum number of records that need to be deleted from the table.

  • DELETE with LIMIT without WHERE condition: It deletes the no. of initial rows as specified by the LIMIT clause.
  • DELETE with LIMIT with WHERE condition: It first applies WHERE condition, then after filtering out, it deletes those many initial rows specified by the LIMIT clause.
  • DELETE with LIMIT with ORDER BY clause: It selects the rows with matching condition if there is a WHERE condition, then sorts the rows based on specified column, and then it deletes the maximum rows specified by the limit clause.

ORDER OF EXECUTION

FROM

->WHERE (Optional)

-> ORDER BY (Optional)

-> LIMIT

Example: To Delete the 2 Student Records Who Secured the Least Marks in the Class

output before deletion for delete with limit clause.

Output Before Deletion for DELETE With LIMIT Clause.

Query:

DELETE FROM Marks WHERE marks<=50 ORDER BY MARKS ASC LIMIT 2;

As we can see, first the query selected rows which have marks<=50, i.e. “Rahul”, “Gill”, “Shami”, and “Rahane”. Then it ordered the rows in ascending order based on the marks i.e.

Shami, 34
Rahul, 47
Rahane, 48
Gill, 50

Output:

Now, it applies the limit condition and deletes the first 2 rows. i.e., “Shami” and “Rahul” as shown below.

output after deletion for delete with limit clause

Output After Deletion for DELETE With LIMIT Clause.

MySQL DELETE with JOIN Clause

MySQL allows us to delete rows from multiple tables based on the matching condition. We use JOIN Clause to first joins the tables based on the join condition and then deletes the rows from both tables.

If only one table is specified, then rows from only one table are deleted.

Sequence of Execution:

FROM->

JOIN->

WHERE->

DELETE.

Example

Initially, we created a CUSTOMERS table with columns id, name, and contact.

Customers-Table

Customers Table.

Next, we have created another table ORDERS with columns customer_id, order_id, order_name, and order_price. It contains all the orders placed by the customers in the customers table.

Orders-Table

Orders Table.

Problem Statement: We want to delete the rows of customer with customer Id “156” in CUSTOMERS table and his associated orders in ORDERS table.

Query:

DELETE  customers, orders 
FROM customers
INNER JOIN orders ON customers.id=orders.customer_id
WHERE customers.id=156;

Output:

As we can see in the below output, the customer record with id “156” is deleted in the customers table and orders related with customer_id “156” are also deleted in the Orders table.

The query will first join the tables based on the join condition, then it will execute the WHERE condition and select those rows whose id is “156”. Finally, it will delete those rows from both the table.

Note: If you omit customers or orders after the DELETE keyword, then rows will be deleted only from the specified table.

Customers:

Customers-Table-After-DELETE-with-JOIN

Customers Table After DELETE with JOIN

Orders:

Orders-Table-After-DELETE-with-JOIN

Orders Table After DELETE with JOIN.

Conclusion

The MySQL DELETE statement is a powerful tool for managing database records. It can delete specific rows or entire tables, but its changes are often permanent, so caution is necessary. Always use a WHERE clause to avoid unintentional deletions, back up your data beforehand, and consider logical deletion methods for safer data management. Understanding its syntax and use cases ensures efficient and safe data handling.

FAQs on MySQL DELETE Statement

What is the MySQL DELETE statement?

The MySQL DELETE statement is used to remove one or more records from a table. It can target specific rows using a WHERE clause or delete all rows when the WHERE clause is omitted.

Can I rollback a DELETE operation in MySQL?

Generally, the DELETE operation is irreversible and cannot be rolled back unless the operation is performed within a transaction and the transaction is not yet committed.

How do I delete specific rows in MySQL?

Use the DELETE statement with a WHERE clause to specify the conditions for the rows you want to delete. For example:

DELETE FROM table_name WHERE condition;

How can I delete all rows from a table in MySQL?

Simply use the DELETE statement without a WHERE clause to delete all rows:

DELETE FROM table_name;

What precautions should I take when using the DELETE statement?

Always use a WHERE clause to avoid deleting all data unintentionally, and back up your database before performing delete operations.



Previous Article
Next Article

Similar Reads

How to Use PL SQL Insert, Update, Delete And Select Statement?
PL/SQL is a powerful extension of SQL specifically designed for Oracle databases. It enables developers to create procedural logic and execute SQL commands within Oracle database environments. In this article, we will explore the usage of fundamental PL/SQL statements such as INSERT, UPDATE, DELETE and SELECT with he help of various examples which
6 min read
SQL DELETE Statement
SQL DELETE is a basic SQL operation used to delete data in a database. SQL DELETE is an important part of database management DELETE can be used to selectively remove records from a database table based on certain conditions. This SQL DELETE operation is important for database size management, data accuracy, and integrity. Syntax:  DELETE FROM tabl
2 min read
MySQL CREATE INDEX Statement
MySQL is an open-source relational database management system that uses Structured Query Language (SQL) to manipulate databases. CREATE INDEX statement is used to retrieve data from the database more quickly. CREATE INDEX statement is used to create an Index that allows MySQL to quickly locate and retrieve relevant data by reducing the need for ful
3 min read
Nested Select Statement in MySQL
The relational databases, the ability to retrieve and manipulate data with precision is a cornerstone of effective database management. MySQL, a popular relational database management system, provides a powerful tool called the Nested SELECT statement that empowers developers to perform complex and versatile data queries. So, In this article we wil
5 min read
MySQL DROP TABLE Statement
MySQL is a free and open-source relational database management system written in C and C++ that is extremely popular among developers. Like other relational database management systems, MySQL provides a variety of rich features to create databases and tables, insert data in them, and further manipulate them as the system evolves. In this article, w
5 min read
How to Call a Stored Procedure Using Select Statement in MySQL?
Stored procedures in MySQL are powerful tools for encapsulating SQL logic and enabling reusability. However, executing stored procedures via SELECT statements can provide additional flexibility, especially when integrating their results directly into queries. This article explores various methods of calling stored procedures using SELECT statements
6 min read
MySQL - ALTER VIEW Statement
The ALTER VIEW statement in MySQL is a powerful tool that allows users to modify the definition of an existing view without the need to drop and recreate it. This statement is particularly useful for changing the query or structure of a view to better help the needs of the application or database design. In this article, We will learn about What is
4 min read
MySQL USE Statement
MySQL is a very flexible and user-friendly Database. The USE command is used when there are multiple databases and we need to SELECT or USE one among them. We can also change to another database with this statement. Thus, the USE statement selects a specific database and then performs queries and operations on it using the inbuilt commands of MySQL
3 min read
MySQL CREATE VIEW Statement
MySQL is a popular open-source relational database management system (RDBMS) that is usually used for developing scalable and high-performance databases. MySQL was developed by MySQL AB (currently owned by Oracle Corporation) in 1995. MySQL is known for its robust, easy, and reliable features with quick processing speeds. MySQL is generally used by
6 min read
MySQL - SHOW VARIABLES Statement
MySQL is an open-source Relational Database Management System that stores data in the form of rows and tables and SQL is known as a programming language that is used to manipulate the data. We can perform many operations in an SQL server with the help of SQL programming language such as manipulating the data, retrieving the data, and storing the da
3 min read