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
 Alternative to using "<>" in WHERE clause

Author  Topic 

Hammerklavier
Starting Member

26 Posts

Posted - 2015-03-10 : 09:37:04
Hi,

I have heard it is a performance hit to use the "not equal to" operator in a where clause, like this:

SELECT * FROM MyTable WHERE TypeID <> 11

If you want everything except for TypeID of 11, what is the preferred way of handling such a query?

Thank you!

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-03-10 : 10:17:46
you heard wrong. However, when you think about it, the only way Sql can satisfy your query is with an index scan (if TypeId is indexed) or a table scan (If Typeid is NOT indexed). It will be better if TypeID is indexed, though
Go to Top of Page

magbenyezi
Starting Member

3 Posts

Posted - 2015-03-11 : 11:41:22
SELECT *
FROM MyTable t
WHERE t.TypeID NOT IN
(SELECT t1.TypeID FROM MyTable t1 WHERE t1.TypeID = 11 )

MA
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2015-03-11 : 11:56:36
quote:
Originally posted by magbenyezi

SELECT *
FROM MyTable t
WHERE t.TypeID NOT IN
(SELECT t1.TypeID FROM MyTable t1 WHERE t1.TypeID = 11 )

MA



Good grief, just no.

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

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-03-11 : 12:09:51
@tkizer LOL!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2015-03-11 : 12:11:40
Gerald, I tried replying to your email from like 3 weeks ago, but it bounced back. Just FYI.

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

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-03-11 : 12:19:05
@tkizer thanks. I'll check my profile (and fix it!)
Go to Top of Page
   

- Advertisement -