Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««1234»»»

The Pitfall of "Not Equal To" Operator in Queries! Expand / Collapse
Author
Message
Posted Wednesday, August 23, 2006 9:23 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, June 5, 2012 12:03 PM
Points: 117, Visits: 163
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 s
WHERE 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 s
WHERE s.StID NOT IN (SELECT StID FROM dbo.StudentExam WHERE ExamName = 'SQL Server')
Post #303547
Posted Wednesday, August 23, 2006 9:38 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Thursday, December 4, 2014 10:11 AM
Points: 892, Visits: 1,560

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 & rude behaivor.  Is it necessary to sue workds like 'stupid' in a constructive criticism?

 

 



Kindest Regards,

Just say No to Facebook!
Post #303557
Posted Wednesday, August 23, 2006 9:58 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Tuesday, October 28, 2014 4:01 PM
Points: 659, Visits: 6,105

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:

SELECT s.*
FROM Students s
where
 not exists ( select stid from studentExam se where s.StID = stid and examname = 'SQL Server' )

Post #303565
Posted Wednesday, August 23, 2006 10:04 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 1:20 PM
Points: 1,276, Visits: 1,135

"ANY COMPARISON OF A NON-NULL TO A NULL IS NULL"

Actually comparisons of a NULL (or a non-NULL value) to a NULL results in UNKNOWN.  Your point is taken though:  The WHERE clause only returns results for rows comparisons that evaluate to TRUE.  Comparisons that evaluate to FALSE or UNKNOWN are dropped from the results.

Post #303569
Posted Wednesday, August 23, 2006 11:21 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, June 12, 2009 7:37 AM
Points: 125, Visits: 115

I 100% agree with the comment made by Duray AKAR.  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 Duray AKAR.

Tim

Post #303587
Posted Wednesday, August 23, 2006 12:10 PM
SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: Moderators
Last Login: Friday, November 14, 2014 10:06 AM
Points: 8,370, Visits: 745

The issue isn't the use of <> or != (which I prefer the later). The pitfall is in presumption of the data and our understanding of the question.

The question being asked that fits your final solution is

"Which students haven't taken the %s exam?"

In your first solution the question is more to the effect

"List the students who have taken exams each time but do not show the times the %s exam was taken."

If you had used left join

"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."

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.




Post #303597
Posted Wednesday, August 23, 2006 12:11 PM
SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: Moderators
Last Login: Friday, November 14, 2014 10:06 AM
Points: 8,370, Visits: 745
Sorry, still a good contribution thou.


Post #303598
Posted Wednesday, August 23, 2006 12:44 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, November 3, 2014 12:25 PM
Points: 26, Visits: 173

y <> x and x <> z then y could possibly = z

This is not a pitfall, but merely an oversight sometimes.

 

Post #303601
Posted Wednesday, August 23, 2006 1:01 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: 2 days ago @ 3:03 PM
Points: 147, Visits: 551

"Actually comparisons of a NULL (or a non-NULL value) to a NULL results in UNKNOWN.  Your point is taken though:  The WHERE clause only returns results for rows comparisons that evaluate to TRUE.  Comparisons that evaluate to FALSE or UNKNOWN are dropped from the results."

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.


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.


I refrained from bringing up the ANSI_NULLS setting before, but it adds an interesting twist.  According to BOL:

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.

and

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.

To confirm this, let's try some simple TSQL.  Running the following produces what you'd expect (after reading BOL).

set ANSI_NULLS OFF
declare @string varchar(25)
set @string = 'TEST'
if @string <> NULL --compare a variable to explicit NULL
print 'true'
else
print 'not true'

set @string = NULL
if @string <> 'TEST' --compare a NULL variable to an explicit value
print 'true'
else
print 'not true'

SELECT 'true' where @string <> 'TEST' --try a comparison in a select.


Results
----
true
true

true

(1 row(s) affected)

set ANSI_NULLS ON and you get
Results
----
not true
not true


(0 row(s) affected)


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?

set ANSI_NULLS OFF
SELECT * FROM Students s left outer join StudentExam se ON s.StID = se.StID WHERE se.ExamName<>'SQL Server'

Results
----
StID StName StID ExamName
2 Anna 2 VB.NET
2 Anna 2 C#.NET
1 Jack 1 XML

(3 row(s) affected)

Huh? Where is Bob?  Hmmm.  Let's continue to use ANSI_NULLS OFF and look for ExamName = NULL

set ANSI_NULLS OFF
SELECT * FROM Students s left outer join StudentExam se ON s.StID = se.StID WHERE se.ExamName=NULL

Results
----
StID StName StID ExamName
3 Bob  

(1 row(s) affected)

There's Bob! 

WHAT'S GOING ON HERE?  How come se.ExamName=NULL evaluates to TRUE but se.ExamName<>'SQL Server' doesn't (with ANSI_NULLS OFF).

Is it because the NULL is "virtual" in the left join.  Let's try one more experiment:

Insert Students VALUES (4,NULL)

set ANSI_NULLS OFF
SELECT * FROM STUDENTS WHERE StName = NULL

Results
----
StID StName

(1 row(s) affected)

So far so good.  Let's try the dratted "not equals to" operator that started this whole mess:

set ANSI_NULLS OFF
SELECT * FROM STUDENTS WHERE StName <> 'Nobody'

Results
----
StID StName
1 Jack
2 Anna
3 Bob

(3 row(s) affected)

WHAT?  Where's the new row we just inserted?  Must be that pesky "not equals to" operator.  Double negative anyone?

set ANSI_NULLS OFF
SELECT * FROM STUDENTS WHERE not StName <> NULL

Results
----
StID StName

(1 row(s) affected)

Sure, there it is.  So it must not be the "not equals to" operator?


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 <> OPERATOR AND NULLS.

 




Post #303604
Posted Wednesday, August 23, 2006 6:14 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, February 12, 2007 2:10 PM
Points: 94, Visits: 1

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"

 select s.* from #students s
where not exists
(
select 1 from #StudentExam where stid=s.stid and Examname='SQL Server'
)

 

Post #303655
« Prev Topic | Next Topic »

Add to briefcase ««1234»»»

Permissions Expand / Collapse