snippetsqlMinor
How do I replace this where clause with a join?
Viewed 0 times
thiswithwherereplacejoinhowclause
Problem
Typically when I see SQL that uses something like:
I replace the
Is it possible to do the same with the inverse in case its is a
Additional considerations
Can I do this:
select * from employees where epmloyeeTypeId in (select id from type where name = 'emp')I replace the
where with this:select e.* from employees e
inner join type t on t.id=e.epmloyeeTypeId and t.name = 'emp'Is it possible to do the same with the inverse in case its is a
not in (like below) instead of an in clause?INSERT into Subscriptions(ProjectId, RecordTypeCID, NTID, Active, Added, LastUpdate, UpdateBy)
SELECT @ProjectId, RecordTypeCID, @NTID, 1, GETDATE(), GETDATE(), @NTID
FROM @Check CHK
WHERE CHK.ActiveStatus=1
And Not Exists (SELECT SubscriptionId FROM Subscriptions
WHERE ProjectId=@ProjectId
and NTID=@NTID
and RecordTypeCID = CHK.RecordTypeCID
)Additional considerations
Can I do this:
INSERT INTO Subscriptions(ProjectId, RecordTypeCID, NTID,Active, Added, LastUpdate, UpdateBy)
SELECT @ProjectId, RecordTypeCID, @NTID,1, GETDATE(), GETDATE(), @NTID
FROM @Check CHK
LEFT JOIN Subscriptions subs ON subs.RecordTypeCID = CHK.RecordTypeCID
AND NTID = @NTID
AND ProjectId = @ProjectId
AND CHK.ActiveStatus = 1
AND subs.SubscriptionId IS NULLSolution
Your NOT EXISTS is more efficient in most cases.
LEFT JOIN internally matches all rows then filters to IS NULL. NOT EXISTS doesn't. This row multiplication also happens in all JOIN based code so you may need an extra aggregate (DISTINCT) to fix the output
NOT IN is generally wrong because NULLs cause no match.
You can also use EXCEPT which gives the same plan as NOT EXISTS.
FYI, as per the SQL-92 standard (page 191, Case 3a) the SELECT bit of the EXISTS is ignored.
LEFT JOIN internally matches all rows then filters to IS NULL. NOT EXISTS doesn't. This row multiplication also happens in all JOIN based code so you may need an extra aggregate (DISTINCT) to fix the output
NOT IN is generally wrong because NULLs cause no match.
You can also use EXCEPT which gives the same plan as NOT EXISTS.
SELECT @ProjectId, RecordTypeCID, @NTID,1, GETDATE(), GETDATE(), @NTID
FROM @Check CHK
EXCEPT
SELECT @ProjectId, RecordTypeCID, @NTID,1, GETDATE(), GETDATE(), @NTID
FROM Subscriptions
WHERE ProjectId=@ProjectId
and NTID=@NTIDFYI, as per the SQL-92 standard (page 191, Case 3a) the SELECT bit of the EXISTS is ignored.
Code Snippets
SELECT @ProjectId, RecordTypeCID, @NTID,1, GETDATE(), GETDATE(), @NTID
FROM @Check CHK
EXCEPT
SELECT @ProjectId, RecordTypeCID, @NTID,1, GETDATE(), GETDATE(), @NTID
FROM Subscriptions
WHERE ProjectId=@ProjectId
and NTID=@NTIDContext
StackExchange Database Administrators Q#1215, answer score: 7
Revisions (0)
No revisions yet.