×

MySQL DELETE JOIN

Sometimes join becomes complicated and unwanted. In such cases, we can delete the unwanted joins in the tables. You can delete inner join, left join, right join as per requirement. The delete join query is an essential part to handle and remove multiple joins. The data manipulation requires a delete query to remove complex join columns.

Syntax

The syntax to delete data using inner join shows below.

DELETE table1, table2
FROM table1
INNER JOIN table2 
ON table1.column1 = table2.column1
WHERE join_condition;

The syntax to delete data using left join shows below.

DELETE table1, table2
FROM table1
LEFT JOIN table2 
ON table1.column1 = table2.column1
WHERE table2.column IS NULL;

The syntax to delete data using the right join shows below.

DELETE table1, table2
FROM table2
RIGHT JOIN table1 
ON table2.column1 = table1.column1
WHERE table1.column IS NULL;

Examples of the MySQL delete join

1) Delete the data using the inner join example shows below.

Execute the below query to delete inner join. It would be best if you will use the "ON" keyword to remove a specific column. The "DELETE" keyword is required for deleting given table data. The "WHERE" clause is used to remove the specific row.

mysql> DELETE department, subject from department 
INNER JOIN subject 
ON department.department_id = subject.subject_id 
WHERE department.department_id = 2;

OUTPUT

Execute the below query to get updated table data.

mysql> Select * from department;
DELETE JOIN

The above image shows that the query deletes the second row using the inner join and the delete keyword.

2) Delete the data using the left join example shows below.

Execute the below query to delete left join. It becomes helpful if we use the "ON" keyword to remove a specific column.

mysql> DELETE department, subject from department LEFT JOIN subject 
ON department.department_id = subject.subject_id 
WHERE department.department_id = 2;

OUTPUT

Execute the below query to get updated table data.

mysql> Select * from department;
DELETE JOIN

The above image deletes the table row using the left join and the delete keyword. Here, table data removes the second row.

3) Delete the data using the right join example shows below.

Execute the below query to delete the right join. It is useful if we use the "ON" keyword to remove a specific column.

mysql> DELETE department, subject from department RIGHT JOIN subject 
ON department.department_id = subject.subject_id 
WHERE department.department_id = 2;

OUTPUT

Execute the below query to get updated table data.

mysql> Select * from department;
DELETE JOIN

The above image deletes the table row that contains the department_id 2. The result displays entire rows except for the second department_id.

4) Delete the data using the inner join example shows below.

Execute the below query to delete the required data in which the "WHERE" clause and "ON"   operator use the "less than" condition.

mysql> DELETE department, subject from department INNER JOIN subject 
ON department.department_id < subject.subject_id 
WHERE department.department_id < 3;

OUTPUT

Execute the below query to get updated table data.

mysql> Select * from department;
DELETE JOIN

In the above image, we can see that the data less than the "3" department_id is removed successfully. Hence, the table displays entire rows except less than the third department_id.

5) Delete the data using the left join example shows below.

Execute the below query to delete the required data in which the "WHERE" clause and "ON"   operator use the "less than" condition.

mysql> DELETE department, subject from department LEFT JOIN subject 
ON department.department_id < subject.subject_id 
WHERE department.department_id < 3;

OUTPUT

Execute the below query to get updated table data.

mysql> Select * from department;
DELETE JOIN

In the above image, we can see that the data less than the "3" department_id is removed successfully. Hence the table displays entire rows except less than the third department_id.

6) Delete the data using the left join example shows below.

Execute the below query to delete the required data in which the "WHERE" clause uses the "equal to" condition and the "ON" operator uses the ‘greater than’ condition.

mysql> DELETE department, subject from department LEFT JOIN subject 
ON department.department_id > subject.subject_id 
WHERE department.department_id = 6 OR subject.subject_id = 2;

OUTPUT

Execute the below query to get updated table data.

mysql> Select * from department;
DELETE JOIN

The above image deletes table data using the left join and deletes keyword. Here, table data removes data "equal to" the "6" department_id.


Related Topics

MySQL TAN() Function

Basically, the TAN() function in MySQL is used to give back the tangent of a mentioned number.In any kind of right-angle triangle, the tangent of an angle is the length...

2 minutes read.

MySQL SEC_TO_TIME() function

In this context, we will learn how we can use the MySQL SEC_TO_TIME() function with proper syntax and good examples. Introduction of MySQL SEC_TO_TIME() function This function in MySQL is used to...

2 minutes read.

MySQL CURRENT_DATE() Function

In this context, we will learn how we can use the MySQL CURRENT_DATE() function to get the current date, and we will see in the two formats, such as strings...

2 minutes read.

MySQL ELT() function

In this context, we will learn how we can use the MySQL ELT() function with proper syntax and good examples. Introduction of MySQL ELT() function In the ELT function, the number field...

2 minutes read.

MySQL ROUND() function

In this context, we will learn how we can use the MySQL ROUND() function with proper syntax and good examples. Introduction of MySQL ROUND() function The ROUND() function in MySQL is used...

3 minutes read.

MySQL LOG() function

In this context, we will learn how we can use the MySQL LOG () function with proper syntax and good examples. Introduction of MySQL LOG() function The LOG() function in MySQL is...

3 minutes read.

MySQL Vs MongoDB

What is MongoDB? MongoDB is a distributed, open-source, cross-platform document-based database which was created to scale and develop applications simply. It was created as a NoSQL database by MongoDB Inc. MongoDB gets...

6 minutes read.

MySQL FIELD() function

In this context, we will learn how we can use the MySQL FIELD() function with proper syntax and good examples. Introduction of MySQL FIELD() function The index position of a mentioned value...

3 minutes read.

MySQL Control flow function

The control flow function uses values or operands for logical operations. These functions can works on single and multiple conditions. The control flow function is based on the Boolean expression....

3 minutes read.

MySQL COUNT function

The count function returns the number of rows in the table. This function shows either the entire rows count or the required row count of the table. It determines the...

5 minutes read.

MySQL Advance table Query

MySQL Advance table Query The table is created by index, rows, and columns in the MySQL database. The user saves their information in matrix format. The database requires a query to...

14 minutes read.

MySQL COALESCE function

This statement displays the first non-null value of the table data. The COALESCE function returns a null value when all values of the table are null or do not find...

3 minutes read.

MySQL POWER() function

In this context, we will learn how we can use the MySQL POWER() function with proper syntax and good examples. Introduction of MySQL POWER() function The value of a number raised to...

3 minutes read.

MySQL Invisible Index

Invisible Index MySQL index decides an option to the availability of the index for the query optimizer. This index shows the visible and invisible options to displays the index column in...

3 minutes read.

MySQL ISNULL function

This function is used to check the null value in table data. It returns the result in a Boolean form. If table data is null, then the output becomes 1....

2 minutes read.

MySQL vs Oracle

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL CEIL() function

In this context, we will learn how we can use the MySQL CEIL () function with proper syntax and good examples. Introduction of MySQL CEIL() function Input is taken by the CEIL()...

3 minutes read.

MySQL String Function

The string function is used to maintain and operate string values. This function modifies the string data as per function. We can do the concatenation, conversion, removal and replacing the...

4 minutes read.

MySQL FORMAT() function

In this context, we will learn how we can use the MySQL FORMAT() function with proper syntax and good examples. Introduction of MySQL FORMAT() function This function in MySQL helps to format...

2 minutes read.

MySQL LEAST function

This statement displays the smallest value of the table. The LEAST function returns a null value when the table contains a null value. If the table contains all numerical values,...

2 minutes read.