17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

know thus far? Actually, the very last query we ran has us 90 percent of the way there. Think about it for<br />

a minute: an OUTER JOIN returns a NULL value in the ProductID column wherever there is no match.<br />

What we are looking for is pretty much the same result set we received in the previous query, except that<br />

we want to filter out any records that do have a ProductID, and we want only the special offer name. To<br />

do this, we simply change our SELECT list and add an extra condition to the WHERE clause:<br />

SELECT Description<br />

FROM Sales.SpecialOfferProduct ssop<br />

RIGHT OUTER JOIN Sales.SpecialOffer sso<br />

ON ssop.SpecialOfferID = sso.SpecialOfferID<br />

WHERE sso.SpecialOfferID != 1<br />

AND ssop.SpecialOfferID IS NULL<br />

As expected, we have exactly the same stores that had NULL values before:<br />

Description<br />

------------------------------<br />

Volume Discount over 60<br />

(1 row(s) affected)<br />

There is one question you might be thinking at the moment that I want to answer in anticipation, so that<br />

you’re sure you understand why this will always work. The question is: “What if the discount record<br />

really has a NULL value?” Well, that’s why we built a WHERE clause on the same field that was part of our<br />

JOIN. If we are joining based on the stor_id columns in both tables, then only three conditions can exist:<br />

❑ If the SpecialOfferProduct.SpecialOfferID column has a non-NULL value, then, according to the<br />

ON operator of the JOIN clause, if a special offer record exists, then SpecialOffer.SpecialOfferID<br />

must also have the same value as SpecialOfferProduct.SpecialOfferID (look at the ON ssop<br />

.SpecialOfferID = sso.SpecialOfferID).<br />

❑ If the SpecialOfferProduct.SpecialOfferID column has a non-NULL value, then, according<br />

to the ON operator of the JOIN clause, if a special offer record does not exist, then SpecialOffer<br />

.SpecialOfferID will be returned as NULL.<br />

❑ If the SpecialOfferProduct.SpecialOfferID happens to have a NULL value, and SpecialOffer<br />

.SpecialOfferID also has a NULL value, there will be no join (null does not equal null), and<br />

SpecialOffer.SpecialOfferID will return NULL because there is no matching record.<br />

A value of NULL does not join to a value of NULL. Why? Think about what we’ve already said about comparing<br />

NULLs —a NULL does not equal NULL. Be extra careful of this when coding. One of the more<br />

common questions I am asked is, “Why isn’t this working?” in a situation where people are using an<br />

“equal to” operation on a NULL — it simply doesn’t work because they are not equal. If you want to test<br />

this, try executing some simple code:<br />

IF (NULL=NULL)<br />

PRINT ‘It Does’<br />

ELSE<br />

PRINT ‘It Doesn’’t’<br />

Chapter 4: JOINs<br />

If you execute this, you’ll get the answer to whether your <strong>SQL</strong> <strong>Server</strong> thinks a NULL equals a NULL —<br />

that is, it doesn’t.<br />

95

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!