Introduction to MySQL DELETE JOIN
Mysql delete join is used to delete rows and records from a table and the matching records of another table. Matching records or rows in the join statements totally depends upon the type of join we apply in our query like whether we are applying inner join, outer join, full join etc.
Case Study for MySQL DELETE JOIN
Below we will learn about two different case to study in MySQL DELETE JOIN:
Case #1
Let’s start the explanation of mysql delete join with inner join clause. Inner join clause is used to select the records or rows that match values in another table.
Below is the inner join clause to select the matching records of the given two table table1 and table2:
Select col_name1,col_name2...col_namen from table1 INNER JOIN table2 ON table1.col_name=table2.col_name;
Mysql inner join clause is used to delete rows or records that match values in the table. As for example, the below statement is used to delete records from table table1 and table2 that satisfy a given condition.
Delete table1,table2 from table1 INNER JOIN table2 on table1.joining_col=table2.joining_col where condition;
In the above statement table1.joining_col =table2.joining_col the expression is the matching condition between the two table table1 and table2 that needs to be deleted. Also, the condition followed by the where clause will result in rows in table1 and table2 that needs to be deleted.
Here, if we remove table1 then the delete statement is only applied to table2 and will only delete rows from table2. Similarly, if we remove table2 then the delete statement is only applied to table1and will only delete rows from table1.
Examples to Implement
Below are examples mentioned:
Example #1
To explain delete inner join we have created two tables in the database named table1 and table2 and inserted 5 rows into it.
Code #1 – Creating table1
create table table1(id int primary key auto_increment);
insert into table1 values(100);
insert into table1 values(101);
insert into table1 values(102);
insert into table1 values(103);
insert into table1 values(104);
select * from table1;
Output:
Code #2 – Creating table2
create table table2(id varchar(100) primary key,quantity int not null);
insert into table2(id,quantity) values('p',101);
insert into table2(id,quantity) values('q',102);
insert into table2(id,quantity) values('r',103);
insert into table2(id,quantity) values('s',104);
insert into table2(id,quantity) values('t',105);
select * from table22;
delete table1,table2 from table1 inner join table2 on table2.quantity=table1.id where table1.id=100;
Output: of the above query in the output console:
It indicated that two rows have been deleted.
Example #2
Now we will create another two table student and contact to explain delete inner join:
Code #1: The two tables created is student and contact.
create table student(stud_id int, stud_name varchar(150),city varchar(150));
insert into student values (1,'divya','france');
insert into student values(2,'aman','england');
insert into student values(3,'rahul','london');
insert into student values(4,'ashish','india');
insert into student values(5,'kriti','america');
insert into student values (6,'saanvi','italy');
select * from student;
Output: Of the student table is displayed in the below output console:
Code #2
create table contact (cont_id int, phone_number varchar(150));
insert into contact values (1,'11123456789');
insert into contact values (2,'74848754555');
insert into contact values (3,'64764849383');
insert into contact values (4,'73383834983');
insert into contact values (5,'63789292292');
select * from contact;
delete student,contact from student inner join contact on student.stud_id=contact.col_id where student.stud_id=2;
After the successful execution of the above query the output will be displayed on the output console:
We can also verify our result by using a select query as given below.
Code #2
select * from contact;
Output:
Case #2
Mysql delete joins with left join:
The left join clause is used to select all rows from the left table, even if they have or don’t have matching in the right table. Delete left join table is used to delete rows from the left table that do not have matching records in the right table.
Below is the syntax to of deleting rows with a left join that does not have matching rows in another table:
Delete table1 from table1 LEFT JOIN table2 ON table1.col_name=table2.column2 where table2.col_name is NULL;
Output: of the above query is an empty set as expected.
Example to Implement
We will create 2 table customer and contact and insert values into it to explain delete left join:
Code #1
create table customer (cust_id int, cust_name varchar(100),qualification varchar(100));
insert into customer values (1,'divya','B.arch');
insert into customer values(2,'aman','M.tech');
insert into customer values(3,'ashish','B.tech');
insert into customer values(4,'somya','M.B.B.S');
insert into customer values(1,'sonika','B.com');
select * from customer;
Output: Below is the output of the customer table displayed on the below output console:
Code #2
select * from contact;
Output:
Code #3
Delete customer from customer LEFT JOIN contact ON customer.cust_id=contact.cont_id where phone_number is NULL;
Output: After successful execution of the above query following output will be displayed on the output console.
Conclusion
In this article, we have learned about mysql delete join query. We have learned about how to delete records from two or more tables joined together by a joint clause. Here, in this article we have taken two cases of join i.e inner join and left join. We have also learned about how to select rows from two or more tabled joint together by joint clause before deleting them. For better understanding of the reader this article also provide the screenshots of the output console all of the queries executed.
Recommended Articles
This is a guide to MySQL DELETE JOIN. Here we discuss an introduction to MySQL DELETE JOIN, syntax, case study with query examples to understand better. You can also go through our other related articles to learn more –
12 Online Courses | 10 Hands-on Projects | 92+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses