I have two tables, T1
which is the original table and the T2
which is an exact copy of T1
until I make changes for it.
At the start both T1
and T2
looks as follows:
ID | Event | Event_Date |
---|---|---|
001 | exam – english | 2022-04-04 |
001 | exam – biology | 2021-03-01 |
002 | exam – maths | 2022-05-23 |
002 | exam – english | 2022-05-29 |
003 | exam – chemistry | 2022-04-13 |
004 | exam – maths | 2021-09-15 |
I’ve used the following SQL to alter T2:
DELETE FROM T2
WHERE ID IN (SELECT ID FROM T2 WHERE EVENT_DATE < '2022-01-01')
And I should get something that looks like the following:
ID | Event | Event_Date |
---|---|---|
002 | exam – maths | 2022-05-23 |
002 | exam – english | 2022-05-29 |
003 | exam – chemistry | 2022-04-13 |
What I want to do now is just a quality check to make sure that I have deleted all rows where an ID
has ever had an event_date < 2022-01-01
.
I’ve tried to do the following:
SELECT T1.ID
FROM T1
INNER JOIN T2 ON T1.ID=T2.ID
WHERE T1.EVENT_DATE < '2022-01-01'
But I think that’s just trying to look for dates < 2022-01-01
which shouldn’t be in T2
anyway so I don’t think that this is the correct way of solving the problem.
2
Answers
It’s simple. Just use Left join instead of Inner join in the query that you have mentioned. It will give you your desired output. Also, you need to add ID column of T2 table so that the NULL values corresponding to ID column of T1 table will help you know which ID was deleted from T2.
You can use Left join in place of inner join in the query.
2022-01-01
, and if there’s no match in T2, it will still return T1.ID with aNULL value
for T2.ID.