What approach should I follow to construct my SQL query if I need to select a data exepct some other data?
For example, my
I want so select all the data from the data-base EXCEPT this result-set:
SELECT *
FROM table1
WHERE table1.MarketTYpe = 'EmergingMarkets'
AND IsBigOne = 1
AND MarketVolume = 'MIDDLE'
AND SomeClass = 'ThirdClass'
Should I use
- NOT IN (the aboe result set)
- Or shoudl I get INVERSE of the conditions like != inseat of = etc.
- Or ?
Can you advice?
Use the EXCEPT construct?
Note that
EXCEPTandNOT EXISTSgive the same query plan using “left anti semi joins”.NOT IN (subquery with above)may not give correct results if there are NULL values in the sub-query, hence I wouldn’t useI would avoid negation in the WHERE clause because it isn’t readable straight away
As the comments show on Michael’s answer…
For more on “all rows except some rows”, see these: