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 7: Adding More to Our Queries<br />

It should wind up looking like this:<br />

SELECT Description<br />

FROM Sales.SpecialOffer sso<br />

WHERE sso.SpecialOfferID != 1<br />

AND sso.SpecialOfferID NOT IN<br />

(SELECT SpecialOfferID FROM Sales.SpecialOfferProduct);<br />

This yields exactly the same record.<br />

Correlated Subqueries<br />

Two words for you on this section: Pay attention! This is another one of those little areas that, if you truly<br />

get it, can really set you apart from the crowd. By “get it,” I don’t just mean that you understand how it<br />

works, but also that you understand how important it can be.<br />

Correlated subqueries are one of those things that make the impossible possible. What’s more, they often<br />

turn several lines of code into one and create a corresponding increase in performance. The problem with<br />

them is that they require a substantially different style of thought than you’re probably used to. Correlated<br />

subqueries are probably the single easiest concept in <strong>SQL</strong> to learn, understand, and then promptly<br />

forget, because the concept simply goes against the grain of how you think. If you’re one of the few who<br />

choose to remember it as an option, then you will be one of the few who figure out that hard-to-figure-out<br />

problem. You’ll also be someone with a far more complete tool set when it comes to squeezing every<br />

ounce of performance out of your queries.<br />

How Correlated Subqueries Work<br />

What makes correlated subqueries different from the nested subqueries we’ve been looking at is that the<br />

information travels in two directions rather than one. In a nested subquery, the inner query is processed<br />

only once, and that information is passed out for the outer query, which will also execute just once —<br />

essentially providing the same value or list that you would have provided if you had typed it in yourself.<br />

With correlated subqueries, however, the inner query runs on information provided by the outer query,<br />

and vice versa. That may seem a bit confusing (that chicken or the egg thing again), but it works in a<br />

three-step process:<br />

1. The outer query obtains a record and passes it into the inner query.<br />

2. The inner query executes based on the passed-in value(s).<br />

3. The inner query then passes the values from its results back to the outer query, which uses them<br />

to finish its processing.<br />

Correlated Subqueries in the WHERE Clause<br />

192<br />

I realize that this is probably a bit confusing, so let’s look at it in an example.

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

Saved successfully!

Ooh no, something went wrong!