Javatpoint Logo
Javatpoint Logo

MySQL DELETE JOIN

DELETE query is a sub-part of data manipulation language used for removing the rows from tables. How to delete join in MySQL is a very popular question during the interviews. It is not an easy process to use the delete join statements in MySQL. In this section, we are going to describe how you can delete records from multiple tables with the use of INNER JOIN or LEFT JOIN in the DELETE query.

DELETE JOIN with INNER JOIN

The Inner Join query can be used with Delete query for removing rows from one table and the matching rows from the other table that fulfill the specified condition.

Syntax

The following are the syntax that can be used for deleting rows from more than one table using Inner Join.

Here, the target is a table name from where we want to delete rows by matching the specified condition. Suppose you want to delete rows from table T1 and T2 where student_id = 2, then it can be written as the following statement:

In the above syntax, the target table (T1 and T2) is written between DELETE and FROM keywords. If we omit any table name from there, then the delete statement only removes rows from a single table. The expression written with ON keyword is the condition that matches the rows in tables where you are going to delete.

Example

Suppose we have two table students and contacts that contains the following data:

Table: students

MySQL DELETE JOIN

Table: contacts

MySQL DELETE JOIN

Execute the following query to understand the Delete Join with Inner Join. This statement deletes a row that has the same id in both tables.

After successful execution, it will give the following message:

MySQL DELETE JOIN

Now, run the following query to verify the rows deleted successfully.

You can see that the rows where the student_id=4 is deleted.

MySQL DELETE JOIN MySQL DELETE JOIN

DELETE JOIN with LEFT JOIN

We have already learned the LEFT JOIN clause with SELECT statement that returns all rows from the left(first) table and the matching or not matching rows from another table. Similarly, we can also use the LEFT JOIN clause with the DELETE keyword for deleting rows from the left(first) table that does not have matching rows from a right(second) table.

The following query explains it more clearly where DELETE statement use LEFT JOIN for deleting rows from Table1 that does not have matching rows in the Table2:

In the above query, notice that we will only use Table1 with the DELETE keyword, not both as did in the INNER JOIN statement.

Example

Let us create a table "contacts" and "customers" in a database that contains the following data:

Table: contacts

MySQL DELETE JOIN

Table: customers

MySQL DELETE JOIN

Execute the following statement that removes the customer who does not have a cellphone number:

After successful execution, it will give the following message:

MySQL DELETE JOIN

Now, run the following query to verify the rows deleted successfully.

You can see that the rows where the customer does not have the cellphone number are deleted.

MySQL DELETE JOIN
Next TopicMySQL Unique Key




Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA