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;
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;
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;
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;
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;
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;
The above image deletes table data using the left join and deletes keyword. Here, table data removes data "equal to" the "6" department_id.