Removing items from a table without a link identifier

I need to remove all members who do not have a license assigned to them.

Table        : Columns

Contributors : [id, Name,...]
Licenses     : [id, ContributorId, Name, ...]

Something like that

DELETE FROM Contributors
WHERE
License.ContributorId != Contributor.Id
+3
source share
3 answers

I believe this will work if you are using SQL Server:

DELETE Contributors 
FROM Contributors as c
LEFT JOIN License as l
ON c.id = l.ContributorID
WHERE l.id is null

A good test to perform before deleting is to replace the string DELETE Contributorswith SELECT *. This will show you all the entries that are about to be deleted, so this is a good health check ...

So your sanity check would look like this:

SELECT *
FROM Contributors as c
LEFT JOIN License as l
ON c.id = l.ContributorID
WHERE l.id is null
+2
source
DELETE FROM Contributors
WHERE NOT EXISTS (
  SELECT *
  FROM License
  WHERE License.ContributorId = Contributors.Id)
+3
source
 DELETE FROM Contributors
    WHERE Contributors.Id NOT IN 
    (SELECT DISTINCT License.ContributorId FROM License)
+1
source

Source: https://habr.com/ru/post/1794015/


All Articles