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.

While this works just fine, queries of this type almost always fall into the category of those that can be<br />

done using an inner join rather than a nested SELECT. For example, we could get the same results as the<br />

preceding subquery by running this simple join:<br />

SELECT DISTINCT pp.ProductID, Name<br />

FROM Production.Product pp<br />

JOIN Sales.SpecialOfferProduct ssop<br />

ON pp.ProductID = ssop.ProductID;<br />

For performance reasons, you want to use the join method as your default solution if you don’t have a<br />

specific reason for using the nested SELECT — we’ll discuss this more before the chapter’s done.<br />

<strong>SQL</strong> <strong>Server</strong> is actually pretty smart about this kind of thing. In the lion’s share of situations, <strong>SQL</strong><br />

<strong>Server</strong> will actually resolve the nested subquery solution to the same query plan it would use on the<br />

join — indeed, if you checked the query plan for both the nested subquery and the previous join, you’d<br />

find it was the exact same plan. So, with that in mind, the truth is that most of the time, there really<br />

isn’t that much difference. The problem, of course, is that I just said most of the time. When the query<br />

plans vary, the join is usually the better choice, and thus the recommendation to use that syntax by<br />

default.<br />

Using a Nested SELECT to Find Orphaned Records<br />

This type of nested SELECT is nearly identical to our previous example, except that we add the NOT operator.<br />

The difference this makes when you are converting to join syntax is that you are equating to an<br />

outer join rather than an inner join.<br />

Before we do the nested SELECT syntax, let’s review one of our examples of an outer join from Chapter 4.<br />

In this query, we were trying to identify all the special offers that do not have matching products:<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 />

This returned one row:<br />

Description<br />

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

Volume Discount over 60<br />

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

Chapter 7: Adding More to Our Queries<br />

This is the way that, typically speaking, things should be done (or as a LEFT JOIN). I can’t say, however,<br />

that it’s the way that things are usually done. The join usually takes a bit more thought, so we usually<br />

wind up with the nested SELECT instead.<br />

See if you can write this nested SELECT on your own. Once you’re done, come back and take a look.<br />

191

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

Saved successfully!

Ooh no, something went wrong!