Archive for April 14th, 2009
Missing entries
Comments enabled. I *really* need your comment
From Stack Overflow:
I have a case where I want to choose any database entry that have an invalid
Country,Region, orAreaID.By invalid, I mean an
IDfor a country or region or area that no longer exists in my tables.I have four tables:
Properties,Countries,Regions,Areas.I was thinking to do it like this:
SELECT * FROM Properties WHERE CountryID NOT IN ( SELECT CountryID FROM Countries ) OR RegionID NOT IN ( SELECT RegionID FROM Regions ) OR AreaID NOT IN ( SELECT AreaID FROM Areas )Now, is my query right?
We have two options here:
- Use a
LEFT OUTER JOINwith the three dimension tables (Countries,RegionsandAreas), filtering out non-NULLvalues - Use a
NOT IN/NOT EXISTSapproach shown above
LEFT JOIN intuitively seems to be more elegant and more efficient than subqueries. But is it really?
Read the rest of this entry »
Subscribe in a reader