Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Delete with left join

Author  Topic 

Suhi100
Starting Member

14 Posts

Posted - 2015-02-20 : 13:15:56
Hi,

can someone tell the logic of the below DELETE statement?
It deletes all records where the file_modified column value is smaller then the maximum value in the same column, but not crystal clear how...


delete a
from Staging a
left join (select max(file_modifieddate) as file_modifieddate from Staging) b on
a.file_modifieddate = b.file_modifieddate
where b.file_modifieddate is null


or if someone knows links to similar topics would be helpful

Thanks!



tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2015-02-20 : 14:32:23
It's due to the use of LEFT JOIN with the IS NULL option in the WHERE clause. For the joined columns in a LEFT JOIN, any rows where there wasn't a match will have a NULL value. Any rows where there was a match will have a non-NULL value. So LEFT JOIN/IS NULL is a way to get the rows that would not show up with an INNER JOIN.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Suhi100
Starting Member

14 Posts

Posted - 2015-02-23 : 05:20:27
Perfect, it is clear now
Thanks
Go to Top of Page
   

- Advertisement -