In SQL, we use INNER JOIN to join the data of two tables. In some cases, we delete data using an inner join.
To delete the data by using the inner join, the syntax is given below:
DELETE firstTable
FROM firstTable INNER JOIN secondTable ON
firstTable.attributeName = secondTable.attributeName
WHERE condition
Suppose we have a table Persons
with the following data:
SELECT * FROM Person;
Next, we will create another table named ProjectManager
with the following data:
SELECT * FROM ProjectManager;
We use INNER JOIN to delete the ProjectManager
above age and fetch the date_of_birth
of the Person
to calculate the age at the time of deletion. We will run the following query:
DELETE ProjectManagerFROM ProjectManager INNER JOIN Person ONProjectManager.p_id = Person.person_idWHERE TIMESTAMPDIFF(YEAR, Person.date_of_birth, '2021-01-23') >= 50;
Line 1: Delete data from ProjectManager
.
Line 2–4: We join the ProjectManager
and the Persons
table based on their relation person_id
. Next, we delete the data of the ProjectManager
whose age is above .