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.

Chapter 4: JOINs<br />

16 Mountain-500 Silver Clearance 0.40 988<br />

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

If you were to perform a SELECT * against the discounts table, you’d quickly find that we have included<br />

every row from that table except for SpecialOfferID 1, which we explicitly excluded from our results.<br />

How It Works<br />

We are doing a LEFT JOIN, and the SpecialOffer table is on the left side of the JOIN. But what<br />

about the SpecialOfferProduct table? If we are joining and we don’t have a matching record for the<br />

SpecialOfferProduct table, then what happens? Since it is not on the inclusive side of the JOIN (in<br />

this case, the LEFT side), <strong>SQL</strong> <strong>Server</strong> will fill in a NULL for any value that comes from the opposite side<br />

of the join if there is no match with the inclusive side of the JOIN. In this case, all but one of our rows<br />

have ProductIDs. What we can discern from that is that all of our SpecialOffers are associated with<br />

at least one product except one (SpecialOfferID 6).<br />

We’ve answered the question then; of the 16 SpecialOffers available, only one is not being used (Volume<br />

Discount over 60).<br />

In this case, switching to a RIGHT JOIN would yield the same thing as the INNER JOIN, as the SpecialOfferProduct<br />

table only contains rows where there is an active link between a special offer and a<br />

product. The concept is, however, exactly the same. We could, for example, switch the order that we reference<br />

the tables and then use a RIGHT JOIN.<br />

Try It Out RIGHT OUTER JOINs<br />

Now, let’s see what happens if we change the question to a RIGHT OUTER JOIN:<br />

SELECT sso.SpecialOfferID, Description, DiscountPct, ProductID<br />

FROM Sales.SpecialOfferProduct ssop<br />

RIGHT OUTER JOIN Sales.SpecialOffer sso<br />

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

WHERE sso.SpecialOfferID != 1<br />

How It Works<br />

If we tried the preceding query with a LEFT JOIN, we would get back just the 243 rows we got with the<br />

INNER JOIN. Run it as presented above, and we get back the unused special offer we wanted.<br />

Finding Orphan or Non-Matching Records<br />

94<br />

We can actually use the inclusive nature of OUTER JOINs to find non-matching records in the exclusive<br />

table. What do I mean by that? Let’s look at an example.<br />

Let’s change our special offer question. We want to know the store names for all the special offers that<br />

are not associated with any products. Can you come up with a query to perform this based on what we

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

Saved successfully!

Ooh no, something went wrong!