﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Discuss Content Posted by Amin Sobati / Article Discussions / Article Discussions by Author  / The Pitfall of "Not Equal To" Operator in Queries! / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 13:16:18 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;&lt;EM&gt;Worth noting that you can use use " select * " rather than " select 1 " or " select 12345 " for that matter.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;I have a sneaking suspicion that using select * or select column could result in a bookmark lookup (or RID lookup) if your query includes columns that are non-clustered index columns or non-indexed columns (particularly in the WHERE clause or in joins).  Select 1 or select 12345 shouldn't encounter that problem.&lt;/P&gt;</description><pubDate>Mon, 03 Sep 2007 09:30:00 GMT</pubDate><dc:creator>Mike C</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;By far the best solution as it relates most closely to the problem to be solved. I also prefer to use "EXISTS" rather than "IN" as you can't use "IN" efficiently if you have a composite key. &lt;/P&gt;&lt;P&gt;Worth noting that you can use use " select * " rather than " select 1 " or " select 12345 " for that matter.&lt;/P&gt;</description><pubDate>Mon, 03 Sep 2007 03:40:00 GMT</pubDate><dc:creator>Alan Nicholas</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;Wow.  Tested it out and got the same results on mine.  Will definitely bear that one in mind.&lt;/P&gt;&lt;P&gt;Thanks Mike!&lt;img src='images/emotions/smile.gif' height='20' width='20' border='0' title='Smile' align='absmiddle'&gt;&lt;/P&gt;</description><pubDate>Tue, 28 Aug 2007 03:05:00 GMT</pubDate><dc:creator>Dave F-425609</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;No problem Dave.  There is a difference between NOT EXISTS and NOT IN in some cases.  Consider the following sample:&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;CREATE TABLE #test (id INT PRIMARY KEY, val VARCHAR(20));&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;INSERT INTO #test (id, val)SELECT 1, 'One'UNION SELECT 2, 'Two'UNION SELECT 3, 'Three'UNION SELECT 4, 'Four'UNION SELECT 5, 'Five';&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;CREATE TABLE #odd (id INT);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;CREATE NONCLUSTERED INDEX ix1ON #odd(id);goWITH Num (n)AS( SELECT 1 UNION ALL SELECT n + 1 FROM Num WHERE n &amp;lt; 10000)INSERT INTO #odd (id)SELECT nFROM NumOPTION (MAXRECURSION 0)goSELECT *FROM #oddWHERE id NOT IN ( SELECT id FROM #test);goSELECT *FROM #oddWHERE NOT EXISTS ( SELECT id FROM #test WHERE #test.id = #odd.id);godrop table #test;drop table #odd;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;On my computer it generates two different query plans.  NOT EXISTS generates a query plan with a cost of 0.0614788.  NOT IN generates a query plan with substantially higher cost of 0.121561.  For small data sets in both tables the two queries generated the same query plan.&lt;/P&gt;&lt;P&gt;EXISTS is not just about breaking the loop, the SELECT in EXISTS uses a correlated subquery and can potentially take advantage of indexes on the table.  In the best case this could result in efficient index seeks in the subquery, depending on which query plan SQL Server decides to go with.&lt;/P&gt;</description><pubDate>Fri, 24 Aug 2007 17:56:00 GMT</pubDate><dc:creator>Mike C</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;Cheers for that Mike.  I'm coming from an Oracle background and in older editions OUTER JOIN=Full Table Scan.&lt;/P&gt;&lt;P&gt;I spent a bit more time looking into EXISTS after my post, and see what you mean.  When you code EXISTS rather than IN the first time it hits success it breaks the analysis loop.  This will be quicker if you are bringing back a huge number of rows from the subquery.&lt;/P&gt;&lt;P&gt;HOWEVER, by the same rational NOT EXISTS should not be quicker than NOT IN AS it would have to scan the entire result set to ensure a single occurence isn't present.  Seems to follow... &lt;img src='images/emotions/unsure.gif' height='20' width='20' border='0' title='Unsure' align='absmiddle'&gt;&lt;/P&gt;</description><pubDate>Fri, 24 Aug 2007 04:46:00 GMT</pubDate><dc:creator>Dave F-425609</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;SPAN id=Showtread1_ThreadRepeater__ctl10_lblFullMessage&gt;&lt;P&gt;&lt;SPAN id=Postmessage1_ucMessageControl_ReplyMsgRepeater__ctl1_lblFullMessage&gt;&lt;EM&gt;Can anyone explain, in detail, why the outer join solution is faster than the Subquery solution in this example?  Is the Exists method fastest of all?&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;It depends...  One could be faster than another in different situations.  It all boils down to which query plan SQL Server decides to use for a given query and a given set of data.  As a side not, on SQL 2000 EXISTS tends to be faster than the IN operator.  The explanation I've seen for this is that the IN operator needs to serialize the entire subquery before it can perform the comparison whereas the EXISTS operator does not.  This behavior may be changed in SQL 2005, although I can't test it myself right now.&lt;/P&gt;&lt;P&gt;&lt;EM&gt;I've examined all three on a limited number of rows and the performance and the plans are identical.  Why do you reckon one will be faster than the next with table growth?&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;Basically it's all up to the optimizer.  It takes a lot of factors into consideration, including amount of data, when generating query plans.  Changing the amount of data could change the query plan it chooses.&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Correct me if I'm wrong, but when you do a left outer join the left part of the join has a full table scan.  How is this faster than a subquery based on an index compared to another column based upon an index?  Also, I can't see the difference in using EXISTS over a standard subquery as it still is effectively comparing values inside the EXISTS subquery?&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;Not necessarily a "full table scan", if it's properly indexed.  You could end up with an (clustered) index scan, which would be more efficient in most cases.  The rationale against the subquery versus EXISTS might be the same as for EXISTS versus IN.  If SQL Server has to serialize the entire subquery before using it, it could be less efficient than using EXISTS.&lt;/SPAN&gt;&lt;/P&gt;&lt;/SPAN&gt;</description><pubDate>Thu, 23 Aug 2007 09:46:00 GMT</pubDate><dc:creator>Mike C</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;Sorry, let me rephrase the question:&lt;/P&gt;&lt;P&gt;&lt;SPAN id=Postmessage1_ucMessageControl_ReplyMsgRepeater__ctl1_lblFullMessage&gt;&lt;EM&gt;Can anyone explain, in detail, why the outer join solution is faster than the Subquery solution in this example?  Is the Exists method fastest of all?&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;I've examined all three on a limited number of rows and the performance and the plans are identical.  Why do you reckon one will be faster than the next with table growth?&lt;/P&gt;&lt;P&gt;Correct me if I'm wrong, but when you do a left outer join the left part of the join has a full table scan.  How is this faster than a subquery based on an index compared to another column based upon an index?  Also, I can't see the difference in using EXISTS over a standard subquery as it still is effectively comparing values inside the EXISTS subquery?&lt;/P&gt;&lt;P&gt;Also the LEFT JOIN thing looks nice, but how would you work it if (as it would seem a more reasonable requirement) you wanted the people who hadn't taken the SQL Server exam BUT HAD TAKEN another?&lt;/SPAN&gt;&lt;/P&gt;</description><pubDate>Thu, 23 Aug 2007 03:47:00 GMT</pubDate><dc:creator>Dave F-425609</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;Can anyone explain, in detail, why the outer join solution is faster than the Subquery solution in this example?  Is the Exists method fastest of all?&lt;/P&gt;</description><pubDate>Thu, 23 Aug 2007 03:38:00 GMT</pubDate><dc:creator>Dave F-425609</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;If you are running SQL 2005 add the index below and see what plan the engine picks...&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;create&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;nonclustered&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;index&lt;/FONT&gt;&lt;FONT size=2&gt; IX_Exam_name &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;on&lt;/FONT&gt;&lt;FONT size=2&gt; StudentExam&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;(&lt;/FONT&gt;&lt;FONT size=2&gt;ExamName&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;)&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;include&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;(&lt;/FONT&gt;&lt;FONT size=2&gt;StID&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;)&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;SELECT&lt;/FONT&gt;&lt;FONT size=2&gt; s&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;.*&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;FROM&lt;/FONT&gt;&lt;FONT size=2&gt; Students s&lt;/P&gt;&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;JOIN&lt;/FONT&gt;&lt;FONT size=2&gt; StudentExam se&lt;/P&gt;&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;ON&lt;/FONT&gt;&lt;FONT size=2&gt; s&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;.&lt;/FONT&gt;&lt;FONT size=2&gt;StID&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt;se&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;.&lt;/FONT&gt;&lt;FONT size=2&gt;StID&lt;/P&gt;&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;WHERE&lt;/FONT&gt;&lt;FONT size=2&gt; se&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;.&lt;/FONT&gt;&lt;FONT size=2&gt;ExamName&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;&amp;lt;&amp;gt;&lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;'SQL Server'&lt;/P&gt;&lt;/FONT&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;Here's the plan with the index for the original != query...&lt;/P&gt;&lt;P&gt;SELECT s.* FROM Students s   JOIN StudentExam se   ON s.StID=se.StID    WHERE se.ExamName&amp;lt;&amp;gt;'SQL Server'  |--Nested Loops(Inner Join, OUTER REFERENCES&lt;img src='images/emotions/sad.gif' height='20' width='20' border='0' title='Sad' align='absmiddle'&gt;[se].[StID]))       |--Index Seek(OBJECT&lt;img src='images/emotions/sad.gif' height='20' width='20' border='0' title='Sad' align='absmiddle'&gt;[TestingDB].[dbo].[StudentExam].[ix_Exam_name] AS [se]), SEEK&lt;img src='images/emotions/sad.gif' height='20' width='20' border='0' title='Sad' align='absmiddle'&gt;[se].[ExamName] &amp;lt; 'SQL Server' OR [se].[ExamName] &amp;gt; 'SQL Server') ORDERED FORWARD)       |--Clustered Index Seek(OBJECT&lt;img src='images/emotions/sad.gif' height='20' width='20' border='0' title='Sad' align='absmiddle'&gt;[TestingDB].[dbo].[Students].[PK__Students__345EC57D] AS [s]), SEEK&lt;img src='images/emotions/sad.gif' height='20' width='20' border='0' title='Sad' align='absmiddle'&gt;[s].[StID]=[TestingDB].[dbo].[StudentExam].[StID] as [se].[StID]) ORDERED FORWARD)&lt;/P&gt;</description><pubDate>Thu, 24 Aug 2006 18:12:00 GMT</pubDate><dc:creator>john hill</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;Thanks Eva,&lt;/P&gt;&lt;P&gt;All of students appear again! You have replaced the NULL with new value, but the logic is the same!&lt;/P&gt;</description><pubDate>Thu, 24 Aug 2006 12:53:00 GMT</pubDate><dc:creator>Amin Sobati</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;I didn't test it but is seems this should work and is a bit simpler:&lt;/P&gt;&lt;P&gt;SELECT s.* FROM Students sLEFT JOIN StudentExam se	ON s.StID=se.StID		WHERE ISNULL(se.ExamName,'') &amp;lt;&amp;gt; 'SQL Server'&lt;/P&gt;&lt;P&gt;This way any NULLs would be replaced with '' and therefore would not include 'SQL Server'.&lt;/P&gt;&lt;P&gt;Eva&lt;/P&gt;</description><pubDate>Thu, 24 Aug 2006 11:41:00 GMT</pubDate><dc:creator>evaleah</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;&lt;EM&gt;"Actually comparisons of a &lt;FONT color=#1111ff&gt;NULL&lt;/FONT&gt; (or a non-&lt;FONT color=#3333dd&gt;NULL&lt;/FONT&gt; value) to a &lt;FONT color=#1111ff&gt;NULL&lt;/FONT&gt; results in UNKNOWN.  Your point is taken though:  The &lt;FONT color=#1111ff&gt;WHERE&lt;/FONT&gt; clause only returns results for rows comparisons that evaluate to TRUE.  Comparisons that evaluate to FALSE or UNKNOWN are dropped from the results."&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;Well now we're splitting hairs!&lt;/EM&gt;&lt;/STRONG&gt; &lt;/P&gt;&lt;P&gt;Actually &lt;FONT color=#1111ff&gt;UNKNOWN&lt;/FONT&gt; represents a possible &lt;EM&gt;result of a logical comparison&lt;/EM&gt; while &lt;FONT color=#1111ff&gt;NULL&lt;/FONT&gt; represents a missing/unknown &lt;EM&gt;value&lt;/EM&gt;.  There can be some confusion with the terminology, but &lt;FONT color=#1111ff&gt;NULL&lt;/FONT&gt; is actually not the same thing as &lt;FONT color=#1111ff&gt;UNKNOWN&lt;/FONT&gt;.  Part of the confusion pops up because the word "&lt;EM&gt;unknown&lt;/EM&gt;" is so often used in the definition of &lt;FONT color=#1111ff&gt;NULL&lt;/FONT&gt;s.&lt;/P&gt;&lt;P&gt;As an example (and to add to the confusion) consider the ANSI SQL:1999 standard which added a &lt;FONT color=#1111ff&gt;BOOLEAN&lt;/FONT&gt; data type to SQL.  The &lt;FONT color=#1111ff&gt;BOOLEAN&lt;/FONT&gt; data type can hold one of three possible values:  &lt;FONT color=#1111ff&gt;TRUE&lt;/FONT&gt;, &lt;FONT color=#1111ff&gt;FALSE&lt;/FONT&gt; and &lt;FONT color=#1111ff&gt;UNKNOWN&lt;/FONT&gt;:  SQL three-valued logic and all, so far so good.&lt;/P&gt;&lt;P&gt;However it also has to be nullable, and to be consistent with the rest of the SQL model, &lt;FONT color=#1111ff&gt;NULL&lt;/FONT&gt; would have to indicate that you don't know whether the &lt;FONT color=#1111ff&gt;BOOLEAN&lt;/FONT&gt; column or variable is &lt;FONT color=#1111ff&gt;TRUE&lt;/FONT&gt;, &lt;FONT color=#1111ff&gt;FALSE&lt;/FONT&gt; or &lt;FONT color=#1111ff&gt;UNKNOWN&lt;/FONT&gt;.  I'll give a quick example.  Before someone points it out, I know this won't actually run on T-SQL... this is "&lt;EM&gt;pseudo-SQL&lt;/EM&gt;" to illustrate what it &lt;EM&gt;might&lt;/EM&gt; look like if ANSI &lt;FONT color=#1111ff&gt;BOOLEAN&lt;/FONT&gt;s were implemented in T-SQL:&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#1111ff&gt;DECLARE&lt;/FONT&gt; @x &lt;FONT color=#1111ff&gt;BOOLEAN&lt;/FONT&gt;;&lt;FONT color=#1111ff&gt;DECLARE&lt;/FONT&gt; @y &lt;FONT color=#1111ff&gt;BOOLEAN&lt;/FONT&gt;;&lt;FONT color=#1111ff&gt;DECLARE&lt;/FONT&gt; @z &lt;FONT color=#1111ff&gt;BOOLEAN&lt;/FONT&gt;;&lt;FONT color=#1111ff&gt;SELECT&lt;/FONT&gt; @x = &lt;FONT color=#1111ff&gt;UNKNOWN&lt;/FONT&gt;;     &lt;FONT color=#119999&gt;-- UNKNOWN&lt;/FONT&gt;&lt;FONT color=#1111ff&gt;SELECT&lt;/FONT&gt; @y = (10 &amp;lt; &lt;FONT color=#1111ff&gt;NULL&lt;/FONT&gt;); &lt;FONT color=#119999&gt;-- evaluates to UNKNOWN&lt;/FONT&gt;&lt;FONT color=#1111ff&gt;SELECT&lt;/FONT&gt; @z = &lt;FONT color=#1111ff&gt;NULL&lt;/FONT&gt;;        &lt;FONT color=#119999&gt;-- NULL value&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;According to this example @x is equal to @y (they are both &lt;FONT color=#1111ff&gt;UNKNOWN&lt;/FONT&gt;).  However, is @x equal to @z?  We don't know the value of @z (is it &lt;FONT color=#1111ff&gt;TRUE&lt;/FONT&gt;, &lt;FONT color=#1111ff&gt;FALSE&lt;/FONT&gt; or &lt;FONT color=#1111ff&gt;UNKNOWN&lt;/FONT&gt;?), but we do know that @x is &lt;FONT color=#1111ff&gt;UNKNOWN&lt;/FONT&gt;.  So is &lt;FONT color=#1111ff&gt;UNKNOWN&lt;/FONT&gt; the same as &lt;FONT color=#1111ff&gt;NULL&lt;/FONT&gt;?  According to the SQL logical model the result of this comparison should be &lt;FONT color=#1111ff&gt;UNKNOWN&lt;/FONT&gt;, since we don't know if @z is &lt;FONT color=#1111ff&gt;TRUE&lt;/FONT&gt;, &lt;FONT color=#1111ff&gt;FALSE&lt;/FONT&gt; or &lt;FONT color=#1111ff&gt;UNKNOWN&lt;/FONT&gt;.  According to SQL:1999 the answer could be &lt;FONT color=#1111ff&gt;TRUE&lt;/FONT&gt; since they say to treat &lt;FONT color=#1111ff&gt;UNKNOWN&lt;/FONT&gt; as equivalent to &lt;FONT color=#1111ff&gt;NULL&lt;/FONT&gt;.&lt;/P&gt;&lt;P&gt;To implement this correctly you actually have to re-work the SQL logical model from three-valued logic to four-valued logic everywhere &lt;img src='images/emotions/sad.gif' height='20' width='20' border='0' title='Sad' align='absmiddle'&gt;  The ANSI standard just says to treat &lt;FONT color=#1111ff&gt;UNKNOWN&lt;/FONT&gt;s the same as &lt;FONT color=#1111ff&gt;NULL&lt;/FONT&gt;s, which means you're starting to introduce inconsistent exceptions into the logical model rather than expanding it to accomodate four-valued logic.&lt;/P&gt;&lt;P&gt;As for the &lt;FONT color=#1111ff&gt;ANSI_NULLS&lt;/FONT&gt; setting, I highly recommend leaving it set to the &lt;FONT color=#1111ff&gt;ON&lt;/FONT&gt; position and using the &lt;FONT color=#1111ff&gt;IS NULL&lt;/FONT&gt; and &lt;FONT color=#1111ff&gt;IS NOT NULL&lt;/FONT&gt; operators (or &lt;FONT color=#1111ff&gt;CASE&lt;/FONT&gt; and &lt;FONT color=#1111ff&gt;COALESCE&lt;/FONT&gt;) to check for &lt;FONT color=#1111ff&gt;NULL&lt;/FONT&gt;s.&lt;/P&gt;</description><pubDate>Thu, 24 Aug 2006 07:56:00 GMT</pubDate><dc:creator>Mike C</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;DIV&gt;&lt;FONT face=Arial size=2&gt;Thank Duray! &lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face=Arial size=2&gt;I agree! &lt;/FONT&gt;&lt;FONT face=Arial size=2&gt;But I intended to show a mistranslation between what a human can mean and what a developer might write in TSQL! This really depends on the experience...&lt;/FONT&gt;&lt;/DIV&gt;</description><pubDate>Thu, 24 Aug 2006 04:02:00 GMT</pubDate><dc:creator>Amin Sobati</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;DIV&gt;&lt;FONT face=Arial size=2&gt;Thanks indeed!&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face=Arial size=2&gt;Certainly there can be variations depending on the experience of the developer. I have tried to BOLD a situation that can lead a developer to strange result. This is a real case and I have seen this mistake by some developers during the years on my IT career.&lt;/FONT&gt;&lt;/DIV&gt;</description><pubDate>Thu, 24 Aug 2006 02:30:00 GMT</pubDate><dc:creator>Amin Sobati</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>DISTINCT is definitely a bad idea, performance-wise.  Avoid if at all possible as it usually involves having to sort the result set and then scan through it, making lots of unnecessary overhead that can often be avoided.</description><pubDate>Thu, 24 Aug 2006 02:18:00 GMT</pubDate><dc:creator>Ian Griffiths</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;The best way to think about it as "NOT EXISTS". This is the most logical human approach. You can always convert "not exists" into "left join"&lt;/P&gt;&lt;P&gt; select s.* from #students swhere not exists(select 1 from #StudentExam where stid=s.stid and Examname='SQL Server')&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Wed, 23 Aug 2006 18:14:00 GMT</pubDate><dc:creator>herb walles</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;"Actually comparisons of a &lt;FONT color=#1111ff&gt;NULL&lt;/FONT&gt; (or a non-&lt;FONT color=#3333dd&gt;NULL&lt;/FONT&gt; value) to a &lt;FONT color=#1111ff&gt;NULL&lt;/FONT&gt; results in UNKNOWN.  Your point is taken though:  The &lt;FONT color=#1111ff&gt;WHERE&lt;/FONT&gt; clause only returns results for rows comparisons that evaluate to TRUE.  Comparisons that evaluate to FALSE or UNKNOWN are dropped from the results."&lt;/P&gt;&lt;P&gt;Well now we're splitting hairs!  But, Mike is absolutely right.  BOL defines the result for those comparisons to be "UNKNOWN".  I've always considered NULL and UNKNOWN to mean the same thing and, interestingly enough, when you look up "unknown data" in BOL you get the "Null Values" overview page!  There must be some subtle distinction though, otherwise the authors would have used NULL instead of UNKNOWN.&lt;/P&gt;&lt;P&gt;Sorry to take this thread so far off topic, but here's an interesting puzzler (while we're splitting hairs).  I apologize if this topic has already been covered elsewhere.&lt;/P&gt;&lt;P&gt;I refrained from bringing up the ANSI_NULLS setting before, but it adds an interesting twist.  According to BOL:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;When SET ANSI_NULLS is ON, a comparison in which one or more of the expressions is NULL does not yield either TRUE or FALSE; it yields UNKNOWN.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;and&lt;/P&gt;&lt;P&gt;&lt;EM&gt;When ANSI_NULLS is OFF, comparisons such as ColumnA = NULL return TRUE when ColumnA contains a null value and FALSE when ColumnA contains some value besides NULL. Also, a comparison of two expressions that have both evaluated to null values yields TRUE.&lt;/EM&gt; &lt;/P&gt;&lt;P&gt;To confirm this, let's try some simple TSQL.  Running the following produces what you'd expect (after reading BOL).&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;set ANSI_NULLS OFFdeclare @string varchar(25)set @string = 'TEST'if @string &amp;lt;&amp;gt; NULL --compare a variable to explicit NULLprint 'true'else print 'not true'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;set @string = NULLif @string &amp;lt;&amp;gt; 'TEST' --compare a NULL variable to an explicit valueprint 'true'else print 'not true'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;SELECT 'true' where @string &amp;lt;&amp;gt; 'TEST' --try a comparison in a select.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Results----&lt;FONT face="Courier New"&gt;truetrue&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;true&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;(1 row(s) affected)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;set ANSI_NULLS ON and you getResults----&lt;FONT face="Courier New"&gt;not truenot true&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;(0 row(s) affected)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;So far so good.  It looks like all we have to do to retrieve Bob in Ehsan's example is set ANSI_NULLS OFF, right?&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;set ANSI_NULLS OFFSELECT * FROM Students s left outer join StudentExam se ON s.StID = se.StID WHERE se.ExamName&amp;lt;&amp;gt;'SQL Server'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;Results----StID StName StID ExamName2 Anna 2 VB.NET2 Anna 2 C#.NET1 Jack 1 XML&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;(3 row(s) affected)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Huh? Where is Bob?  Hmmm.  Let's continue to use ANSI_NULLS OFF and look for ExamName = NULL&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;set ANSI_NULLS OFFSELECT * FROM Students s left outer join StudentExam se ON s.StID = se.StID WHERE se.ExamName=NULL&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Results----&lt;FONT face="Courier New"&gt;StID StName StID ExamName3 Bob  &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;(1 row(s) affected)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;There's Bob!  &lt;/P&gt;&lt;P&gt;WHAT'S GOING ON HERE?  How come se.ExamName=NULL evaluates to TRUE but se.ExamName&amp;lt;&amp;gt;'SQL Server' doesn't (with ANSI_NULLS OFF).&lt;/P&gt;&lt;P&gt;Is it because the NULL is "virtual" in the left join.  Let's try one more experiment:&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;Insert Students VALUES (4,NULL)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;set ANSI_NULLS OFFSELECT * FROM STUDENTS WHERE StName = NULL &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Results----&lt;FONT face="Courier New"&gt;StID StName4 &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;(1 row(s) affected)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;So far so good.  Let's try the dratted "not equals to" operator that started this whole mess:&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;set ANSI_NULLS OFFSELECT * FROM STUDENTS WHERE StName &amp;lt;&amp;gt; 'Nobody'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Results----&lt;FONT face="Courier New"&gt;StID StName1 Jack2 Anna3 Bob&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;(3 row(s) affected)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;WHAT?  Where's the new row we just inserted?  Must be that pesky "not equals to" operator.  Double negative anyone?&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;set ANSI_NULLS OFFSELECT * FROM STUDENTS WHERE not StName &amp;lt;&amp;gt; NULL&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Results----&lt;FONT face="Courier New"&gt;StID StName4 &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;(1 row(s) affected)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Sure, there it is.  So it must not be the "not equals to" operator?&lt;/P&gt;&lt;P&gt;My advice to anyone still reading:  BE VERY CAREFUL WITH "SET ANSI_NULLS OFF" AND ANY COMPARISONS WITH NULLS AND BE ESPECIALLY CAREFUL WITH THE &amp;lt;&amp;gt; OPERATOR AND NULLS.&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Wed, 23 Aug 2006 13:01:00 GMT</pubDate><dc:creator>Andy DBA</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;y &amp;lt;&amp;gt; x and x &amp;lt;&amp;gt; z then y could possibly = z&lt;/P&gt;&lt;P&gt;This is not a pitfall, but merely an oversight sometimes.&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Wed, 23 Aug 2006 12:44:00 GMT</pubDate><dc:creator>Ken Shapley</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>Sorry, still a good contribution thou.</description><pubDate>Wed, 23 Aug 2006 12:11:00 GMT</pubDate><dc:creator>Antares686</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;The issue isn't the use of &amp;lt;&amp;gt; or != (which I prefer the later). The pitfall is in presumption of the data and our understanding of the question.&lt;/P&gt;&lt;P&gt;The question being asked that fits your final solution is&lt;/P&gt;&lt;P&gt;"Which students haven't taken the %s exam?"&lt;/P&gt;&lt;P&gt;In your first solution the question is more to the effect&lt;/P&gt;&lt;P&gt;"List the students who have taken exams each time but do not show the times the %s exam was taken."&lt;/P&gt;&lt;P&gt;If you had used left join&lt;/P&gt;&lt;P&gt;"List the students who have taken exams each time but do not show the times the %s exam was taken and make sure to show those who haven't taken an exam at all at least once."&lt;/P&gt;&lt;P&gt;The biggest issue with generating data for reports is always making sure you understand the intended result fully and structure the query correctly. Some syntax is more prone than others to creating the structure that represents the question you want to pose. As always thou there can be dozens of ways to ask the same question and get the correct answer, then it is a matter of most effective way.&lt;/P&gt;</description><pubDate>Wed, 23 Aug 2006 12:10:00 GMT</pubDate><dc:creator>Antares686</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;I 100% agree with the comment made by &lt;A class=smllinks id=Showtread1_ThreadRepeater__ctl4_lnkMessageAuthor title="Click to view users profile..." href="http://www.sqlservercentral.com/forums/userinfo.aspx?id=205521"&gt;&lt;STRONG&gt;Duray AKAR&lt;/STRONG&gt;&lt;/A&gt;.  The query listed will never retrieve the results "the students that has not taken the "SQL Server" exam...will only return those results for students that have taken an exam that is not SQL Server.  Very good point &lt;A class=smllinks id=Showtread1_ThreadRepeater__ctl4_lnkMessageAuthor title="Click to view users profile..." href="http://www.sqlservercentral.com/forums/userinfo.aspx?id=205521"&gt;&lt;STRONG&gt;Duray AKAR&lt;/STRONG&gt;&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;Tim&lt;/P&gt;</description><pubDate>Wed, 23 Aug 2006 11:21:00 GMT</pubDate><dc:creator>Tim Chapman-218780</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;&lt;STRONG&gt;"ANY COMPARISON OF A NON-NULL TO A NULL IS NULL"&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Actually comparisons of a &lt;FONT color=#1111ff&gt;NULL&lt;/FONT&gt; (or a non-&lt;FONT color=#3333dd&gt;NULL&lt;/FONT&gt; value) to a &lt;FONT color=#1111ff&gt;NULL&lt;/FONT&gt; results in UNKNOWN.  Your point is taken though:  The &lt;FONT color=#1111ff&gt;WHERE&lt;/FONT&gt; clause only returns results for rows comparisons that evaluate to TRUE.  Comparisons that evaluate to FALSE or UNKNOWN are dropped from the results.&lt;/P&gt;</description><pubDate>Wed, 23 Aug 2006 10:04:00 GMT</pubDate><dc:creator>Mike C</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;This is a classic example of an exclusion list and point sout the limitiations of the inequality operator. What is presented in this example is that members in one are are to be excluded from from others. (Note that the example is students who have not taken the SQL Server exam. Bob gets omitted. Does he get omitted because he has not taken an exam?).  And as the author points out, "you can simply use a subquery to build the list of students who have taken SQL Server exam" and then build your exclusion using the NOT EXISTS clause: &lt;/P&gt;&lt;P&gt;SELECT s.*FROM Students swhere not exists ( select stid from studentExam se where s.StID = stid and examname = 'SQL Server' )&lt;/P&gt;</description><pubDate>Wed, 23 Aug 2006 09:58:00 GMT</pubDate><dc:creator>Scalability Doug</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;&lt;FONT style="BACKGROUND-COLOR: #fdfed3"&gt;Regardless of the material, a thanks to Amin Sobati  for writing it.  I often wonder why any of these contributing authors would take the time to produce material when too often that maetrial recieves not just constructive criticism but out right crude &amp;amp; rude behaivor.  Is it necessary to sue workds like 'stupid' in a constructive criticism?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT style="BACKGROUND-COLOR: #fdfed3"&gt;&lt;/FONT&gt; &lt;/P&gt;&lt;P&gt;&lt;FONT style="BACKGROUND-COLOR: #fdfed3"&gt;&lt;/FONT&gt; &lt;/P&gt;</description><pubDate>Wed, 23 Aug 2006 09:38:00 GMT</pubDate><dc:creator>YSLGuru</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>The question is one of membership, and the IN clause seems to be a very clear way to pose that question.  After proper indexing, it yields the same query plan as the methods proposed, but in a more understandable query./* Students that have taken the SQL Server test */SELECT s.*FROM dbo.Students AS sWHERE s.StID IN (SELECT StID FROM dbo.StudentExam WHERE ExamName = 'SQL Server')/* Students that have not taken the SQL Server test */SELECT s.*FROM dbo.Students AS sWHERE s.StID NOT IN (SELECT StID FROM dbo.StudentExam WHERE ExamName = 'SQL Server')</description><pubDate>Wed, 23 Aug 2006 09:23:00 GMT</pubDate><dc:creator>Dennis D. Allen</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;If you "Check out" Ehsan's example you'll find Jack is retrieved even though he &lt;EM&gt;did &lt;/EM&gt;take the class and, even though Bob did &lt;EM&gt;not &lt;/EM&gt;take the class, he is not retrieved.&lt;/P&gt;&lt;P&gt;"Stupid people compose a query and expect other unusual result &lt;IMG title=Tongue height=20 src="http://www.sqlservercentral.com/forums/images/emotions/tongue.gif" width=20 align=absMiddle border=0&gt;"&lt;/P&gt;&lt;P&gt;Vladan's variant 1 example returns the correct results because the ExamName criteria is in the join, &lt;EM&gt;NOT&lt;/EM&gt; the where clause.  This causes all StudentExam columns in the join's result set to be NULL for students without a 'SQL Server' exam.  The example then filters out the non-null rows from the result set with the where clause.&lt;/P&gt;&lt;P&gt;In Ehsan's example, putting the &lt;STRONG&gt;ExamName &amp;lt;&amp;gt; 'SQL Server'&lt;/STRONG&gt; criteria in the where clause filters out all rows where ExamName = 'SQL Server'.  This allows Jack to still be retrieved because there is a row in the result set where ExamName &amp;lt;&amp;gt; 'SQL Server'.&lt;/P&gt;&lt;P&gt;This also causes Bob to be filtered out because all of the StudentExam columns in the result set are NULL for Bob (who didn't take &lt;EM&gt;any&lt;/EM&gt; exams).  &lt;STRONG&gt;ANY COMPARISON OF A NON-NULL TO A NULL IS NULL.  &lt;/STRONG&gt;So, ExamName &amp;lt;&amp;gt; 'SQL Server' evaluates to NULL even though it seems like it should evaluate to TRUE and the row is filtered out.  Bye bye Bob.&lt;/P&gt;&lt;P&gt;There's more good stuff in BOL on NULL comparisons in the "Comparison Search Conditions" subtopic under "null values" in the index.&lt;/P&gt;&lt;P&gt;Finally, Ehsan's example will return duplicates for students taking more than one exam other than 'SQL Server'.  The beauty of Vladan's example is that you only get one row in the result set for each row in the "left" table without a match in the "right" table so "select distinct" is not required.&lt;/P&gt;</description><pubDate>Wed, 23 Aug 2006 09:20:00 GMT</pubDate><dc:creator>Andy DBA</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;Ehsan,&lt;/P&gt;&lt;P&gt;Bob does not show up in your results.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Don&lt;/P&gt;</description><pubDate>Wed, 23 Aug 2006 08:56:00 GMT</pubDate><dc:creator>Donald Eberhart</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>The point of the article is excellent. It reinforces the fact that we as SQL programmers need to be careful and be sure of the results that our queries will return. It's a reminder to us veterans to not be complacent and whip out some crappy code when we're in a hurry. It's a nice lesson for new programmers who could easily fall into the trap of using the "Not Equal To" operator when they should really be using a sub-query or outer join.</description><pubDate>Wed, 23 Aug 2006 08:50:00 GMT</pubDate><dc:creator>Dan Knowlton</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>Subqueries are usually a bad idea as a first option- how about a left join, perhaps with a DISTINCT in the SELECT clause?</description><pubDate>Wed, 23 Aug 2006 08:36:00 GMT</pubDate><dc:creator>Jason Hopkins</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;Here's your sample tables with indexes on them.  Should eliminate the table scans in your query plan.&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#1111ff&gt;CREATE TABLE&lt;/FONT&gt; Students (StID &lt;FONT color=#1111ff&gt;INT NOT NULL PRIMARY KEY NONCLUSTERED&lt;/FONT&gt;,StName &lt;FONT color=#1111ff&gt;NVARCHAR&lt;/FONT&gt;(50) &lt;FONT color=#1111ff&gt;NOT NULL&lt;/FONT&gt;)GO&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#1111ff&gt;CREATE CLUSTERED INDEX&lt;/FONT&gt; IX_Students&lt;FONT color=#1111ff&gt;ON&lt;/FONT&gt; Students (StName)GO&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#1111ff&gt;INSERT&lt;/FONT&gt; Students &lt;FONT color=#1111ff&gt;VALUES&lt;/FONT&gt; (1,'Jack')&lt;FONT color=#1111ff&gt;INSERT&lt;/FONT&gt; Students &lt;FONT color=#1111ff&gt;VALUES&lt;/FONT&gt; (2,'Anna')&lt;FONT color=#1111ff&gt;INSERT&lt;/FONT&gt; Students &lt;FONT color=#1111ff&gt;VALUES&lt;/FONT&gt; (3,'Bob')GO&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#1111ff&gt;CREATE TABLE&lt;/FONT&gt; StudentExam (StID &lt;FONT color=#1111ff&gt;INT NOT NULL&lt;/FONT&gt;,ExamName &lt;FONT color=#1111ff&gt;VARCHAR&lt;/FONT&gt;(50) &lt;FONT color=#1111ff&gt;NOT NULL&lt;/FONT&gt;,&lt;FONT color=#1111ff&gt;PRIMARY KEY&lt;/FONT&gt; (StID, ExamName))GO&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#1111ff&gt;INSERT&lt;/FONT&gt; StudentExam &lt;FONT color=#1111ff&gt;VALUES&lt;/FONT&gt; (1,'SQL Server')&lt;FONT color=#1111ff&gt;INSERT&lt;/FONT&gt; StudentExam &lt;FONT color=#1111ff&gt;VALUES&lt;/FONT&gt; (2,'VB.NET')&lt;FONT color=#1111ff&gt;INSERT&lt;/FONT&gt; StudentExam &lt;FONT color=#1111ff&gt;VALUES&lt;/FONT&gt; (2,'C#.NET')&lt;FONT color=#1111ff&gt;INSERT&lt;/FONT&gt; StudentExam &lt;FONT color=#1111ff&gt;VALUES&lt;/FONT&gt; (1,'XML')GO&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Then run your query and see if it comes back with a more efficient query plan.&lt;/P&gt;</description><pubDate>Wed, 23 Aug 2006 07:39:00 GMT</pubDate><dc:creator>Mike C</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;Here is how I would answer the original question:&lt;/P&gt;&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#3333dd&gt;select&lt;/FONT&gt; s.&lt;FONT color=#777777&gt;*&lt;/FONT&gt; &lt;FONT color=#3333dd&gt;from&lt;/FONT&gt; Students s&lt;FONT color=#3333dd&gt;where&lt;/FONT&gt; &lt;FONT color=#777777&gt;not exists&lt;/FONT&gt;  (&lt;FONT color=#3333dd&gt;select&lt;/FONT&gt; 1 &lt;FONT color=#3333dd&gt;from&lt;/FONT&gt; StudentExam se   &lt;FONT color=#3333dd&gt;where&lt;/FONT&gt; se.ExamName = &lt;FONT color=#ff1111&gt;'SQL Server'&lt;/FONT&gt;     &lt;FONT color=#777777&gt;and&lt;/FONT&gt; s.StID=se.StID)&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;While I have seen some performance issues when using an &lt;FONT face="Courier New"&gt;&lt;FONT color=#777777&gt;in&lt;/FONT&gt; (&lt;FONT color=#3333dd&gt;select&lt;/FONT&gt; ...)&lt;/FONT&gt; clause &lt;FONT face="Courier New" color=#777777&gt;exists&lt;/FONT&gt; seems to work great.&lt;/P&gt;</description><pubDate>Wed, 23 Aug 2006 07:19:00 GMT</pubDate><dc:creator>Tatsu</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;SELECT s.* FROM Students sJOIN StudentExam seON s.StID=se.StIDWHERE se.ExamName&amp;lt;&amp;gt;'SQL Server'&lt;/P&gt;&lt;P&gt;is definitely NOT the query to be used to retrieve:&lt;EM&gt;The students that has &lt;STRONG&gt;not taken "SQL Server" exam&lt;/STRONG&gt; &lt;/EM&gt;in the given data architecture... &lt;/P&gt;&lt;P&gt;It is a query to get: &lt;EM&gt;The students that has &lt;STRONG&gt;taken an exam that is NOT "SQL Server".&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;And it successfully delivers the expected result. What if there are students that has not taken ANY exams ?&lt;/P&gt;&lt;P&gt;So, I don't think this is a pitfall of "&amp;lt;&amp;gt;" operator at all !&lt;/P&gt;&lt;P&gt;The query should be in the final form that the author suggests to begin with....&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Wed, 23 Aug 2006 07:10:00 GMT</pubDate><dc:creator>Duray AKAR</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;P&gt;I'm not sure why one would use precisely this form... IMHO it is more complicated than necessary, and the complication does not bring any advantages :SELECT s.* FROM Students s LEFT JOIN (SELECT StID FROM StudentExam WHERE ExamName='SQL Server') se ON s.StID=se.StIDWHERE se.StID IS NULL&lt;/P&gt;&lt;P&gt;I would prefer to use one of these variants:variant 1) - simplestSELECT s.* FROM Students s LEFT JOIN StudentExam E ON E.stID=s.stID AND ExamName='SQL Server'WHERE E.StID IS NULL&lt;/P&gt;&lt;P&gt;variant 2) - to avoid possible duplicatesSELECT s.* FROM Students s LEFT JOIN (SELECT &lt;FONT color=#dd1111&gt;&lt;STRONG&gt;DISTINCT&lt;/STRONG&gt;&lt;/FONT&gt; StID FROM StudentExam WHERE ExamName='SQL Server') se  ON s.StID=se.StIDWHERE se.StID IS NULL&lt;/P&gt;&lt;P&gt;Is there any reason why to use the code as shown in the article? &lt;/P&gt;&lt;P&gt;Yes I know that if condition is NOT, there can't be any duplicities because I only take those that don't have any corresponding rows... but generally when speaking about similar JOINs, duplicities are things that can cause problems - so I thought I'll mention that... also because it is the main reason why/when I would use the derived table. Otherwise, variant 1 should be good enough.&lt;/P&gt;</description><pubDate>Wed, 23 Aug 2006 05:14:00 GMT</pubDate><dc:creator>Vladan</dc:creator></item><item><title>RE: The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;Nice article.&lt;/P&gt;&lt;P&gt;Another way of writing the same, for those who prefer the old convention (I dont recommend it).&lt;img src='images/emotions/ermm.gif' height='20' width='20' border='0' title='Errmmm...' align='absmiddle'&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;SELECT&lt;/FONT&gt;&lt;FONT size=2&gt; s&lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;.*,&lt;/FONT&gt;&lt;FONT size=2&gt; se&lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;.*&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;FROM&lt;/FONT&gt;&lt;FONT size=2&gt; Students s &lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;,&lt;/P&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;SELECT&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;*&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;FROM&lt;/FONT&gt;&lt;FONT size=2&gt; StudentExam &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;WHERE&lt;/FONT&gt;&lt;FONT size=2&gt; ExamName&lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;=&lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;'SQL Server'&lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;)&lt;/FONT&gt;&lt;FONT size=2&gt; se&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;where&lt;/FONT&gt;&lt;FONT size=2&gt; s&lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;.&lt;/FONT&gt;&lt;FONT size=2&gt;StID &lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;!=&lt;/FONT&gt;&lt;FONT size=2&gt;se&lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;.&lt;/FONT&gt;&lt;FONT size=2&gt;StID&lt;/P&gt;&lt;/FONT&gt;</description><pubDate>Wed, 23 Aug 2006 04:51:00 GMT</pubDate><dc:creator>nileshsane</dc:creator></item><item><title>The Pitfall of "Not Equal To" Operator in Queries!</title><link>http://www.sqlservercentral.com/Forums/Topic302141-319-1.aspx</link><description>Comments posted to this topic are about the content posted at &lt;A HREF="temp"&gt;temp&lt;/A&gt;</description><pubDate>Wed, 16 Aug 2006 09:25:00 GMT</pubDate><dc:creator>Yaroslav Pentsarskyy-353753</dc:creator></item></channel></rss>