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 />

92<br />

What I’m trying to get across here is that the table that comes before the JOIN keyword is considered to<br />

be the LEFT table, and the table that comes after the JOIN keyword is considered to be the RIGHT table.<br />

OUTER JOINs are, as I’ve said, inclusive in nature. What specifically gets included depends on which<br />

side of the join you have emphasized. A LEFT OUTER JOIN includes all the information from the table<br />

on the left, and a RIGHT OUTER JOIN includes all the information from the table on the right. Let’s put<br />

this into practice with a small query so that you can see what I mean.<br />

Let’s say we want to know what all our special offers are, the amount of each discount, and which products,<br />

if any, can have them applied. Looking over the AdventureWorks<strong>2008</strong> database, we have a table<br />

called SpecialOffer in the Sales schema. We also have an associate table called SpecialOfferProduct<br />

that lets us know what special offers are associated with what products:<br />

Sales.SpecialOffer Sales.SpecialOfferProduct<br />

SpecialOfferID SpecialOfferID<br />

Description ProductID<br />

DiscountPct rowguid<br />

Type ModifiedDate<br />

Category<br />

StartDate<br />

EndDate<br />

MinQty<br />

MaxQty<br />

rowguid<br />

ModifiedDate<br />

We can directly join these tables based on the SpecialOfferID. If we did this using a common INNER<br />

JOIN, it would look something like:<br />

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

FROM Sales.SpecialOffer sso<br />

JOIN Sales.SpecialOfferProduct ssop<br />

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

WHERE sso.SpecialOfferID != 1

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

Saved successfully!

Ooh no, something went wrong!