DELETE Statement with INNER JOIN

Now, if we again execute the DELETE INNER JOIN query from the Example 2 tables:

DELETE FROM students
WHERE NOT EXISTS (
SELECT 1
FROM enrollments en
INNER JOIN grades gd ON en.enrollment_id = gd.enrollment_id
WHERE students.student_id = en.student_id
);

It will delete those rows from the students table with no associated enrollments and grades.

After the deletion, the employees table will look like this –

Output:

output_tbl_students

MySQL DELETE JOIN

MySQL is an open-source, user-friendly, powerful, and popular choice, relational database management system. When maintaining and modifying data, tables usually interact in a complex way.

MySQL’s DELETE JOIN function is one of its most powerful functions. MySQL DELETE JOIN is explored in detail in this article, which also offers examples to help visualize its capabilities with Right Join, Left Join, Inner Join, and Subqueries.

Similar Reads

MySQL DELETE JOIN

With the help of the MySQL DELETE JOIN function, we can delete row entries from one table in response to a condition related to another table. This is very useful when we need to delete rows based on specific conditions from data that exists across many linked tables....

Examples of MySQL DELETE JOIN

Let’s have two tables, the first is employees and the other is salaries, those contain the following data:...

DELETE Statement with INNER JOIN

Now, if we again execute the DELETE INNER JOIN query from the Example 2 tables:...

Using Subquery With INNER JOIN

Now, if we again execute the DELETE JOIN in subquery from the Example 2 tables:...

Conclusion

MySQL DELETE JOIN is a strong feature for maintaining data security and cleanliness in complex database structures. Whether using Left Join for inclusive deletions or Inner Join for exact deletions or Right Join for accurate deletions, understanding this operation is important for effective database management. Always exercise caution when performing DELETE operations, especially in complex scenarios, to avoid accidental data loss. Testing on a small dataset or a backup copy of the database is suggested....

FAQs on MySQL DELETE JOIN

What is MySQL DELETE JOIN?...