×

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 Create index

An index creation helps to make a row of the table unique. The index operates and handles table data quickly. MySQL index requires NOT NULL column constraint. The index column...

2 minutes read.

MySQL Triggers

MySQL trigger is a function of the stored procedure to respond to the system program. This function responds and runs any data table event automatically. You can use it for...

5 minutes read.

MySQL DATE_FORMAT() function

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

3 minutes read.

MySQL Math Function

The math function operates numerical values and displays required number data. The math function finds out sin, cos, tan values. Here, you find out the absolute value, reminder, and logarithmic...

6 minutes read.

MySQL Environmental Setup

The MySQL Environmental Setup MySQL is free, open-source, and cross-platform software, which can be downloaded from its official website. MySQL management system installs on Linux, macOS, and windows. MySQL requires a framework...

6 minutes read.

MySQL QUARTER() function

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

3 minutes read.

MySQL Drop Index

Sometimes we have an index that is not required in data operations. In that case, we can use this statement to remove an existing index from the table. We can...

4 minutes read.

MySQL Composite Index

Composite Index A composite index is an index that is used on multiple columns. MySQL management system manages multiple columns simultaneously. Therefore, the single index contains multiple columns in one query...

3 minutes read.

MySQL INSTR() Function

In this context, we will learn how we can use the MySQL INSTR() function with proper syntax and good examples. Introduction of MySQL INSTR() function This function in MySQL returns the location...

4 minutes read.

MySQL EXPORT_SET() function

In this context, we will learn how we can use the MySQL EXPORT_SET() function with proper syntax and good examples. Introduction of MySQL EXPORT_SET() function This function returns a string and shows...

3 minutes read.

MySQL UPDATE JOIN

The UPDATE JOIN is a MySQL statement used to perform cross-table updates that means we can update one table using another table with the JOIN clause condition. The "update join"...

5 minutes read.

MySQL DATEDIFF() function

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

3 minutes read.

MySQL PERIOD_DIFF() function

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

2 minutes read.

MySQL Aggregate function

The aggregation function works on the multiple values and returns single value output.  It makes the advanced and complex operation simple. The summation (SUM), MAX, AVG, MIN, COUNT functions are...

5 minutes read.

MySQL ASIN() Function

In this context we will learn how we can use the ASIN() function in MySQL with proper syntax and good example. Introduction of MySQL ASIN() function The arc sine value of a...

2 minutes read.

MySQL Join

The relational database system needs to interconnect multiple tables with each other. MySQL is a popular and easy data management system to connect multiple tables. The foreign key is used...

5 minutes read.

MySQL HEX() function

In this context, we will learn how we can use the MySQL HEX() function with proper syntax and good examples. Introduction of MySQL HEX() function For returning an equivalent hexadecimal string value...

3 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 SUM function

The SUM() function displays the total addition of the table values. It supports the arithmetic operation of the column values. This function does not return a null value. Here, the...

5 minutes read.