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.

❑ Aliases are used in both queries — even though it looks like the outer query shouldn’t need<br />

one — because they are required whenever you explicitly refer to a column from the other<br />

query (inside refers to a column on the outside or vice versa).<br />

The latter point concerning needing aliases is a big area of confusion. The fact is<br />

that sometimes you need them and sometimes you don’t. While I don’t tend to use<br />

them at all in the types of nested subqueries that we looked at in the early part of<br />

this chapter, I alias everything when dealing with correlated subqueries.<br />

The hard-and-fast rule is that you must alias any table (and its related columns) that’s<br />

going to be referred to by the other query. The problem is that this can quickly become<br />

very confusing. The way to be on the safe side is to alias everything — that way you’re<br />

positive of which table in which query you’re getting your information from.<br />

We see that 19,134 row(s) affected only once. That’s because it affected 19,134 rows only one time.<br />

Just by observation, we can guess that this version probably runs faster than the two-query version and,<br />

in reality, it does. Again, we’ll look into this a bit more shortly.<br />

In this particular query, the outer query references only the inner query in the WHERE clause — it could<br />

also have requested data from the inner query to include in the select list.<br />

Normally, it’s up to us whether to use an alias or not, but with correlated subqueries they are often<br />

required. This particular query is a really great one for showing why, because the inner and outer<br />

queries are based on the same table. Since both queries are getting information from each other without<br />

aliasing, how would they know which instance of the table data that you were interested in?<br />

Correlated Subqueries in the SELECT List<br />

Subqueries can also be used to provide a different kind of answer in your selection results. This kind of<br />

situation is often found where the information you’re after is fundamentally different from the rest of the<br />

data in your query (for example, you want an aggregation on one field, but you don’t want all the baggage<br />

from that to affect the other fields returned).<br />

To test this, let’s just run a somewhat modified version of the query we used in the previous section.<br />

What we’re going to say we’re after here is just the account number of the customer and the first date on<br />

which that customer ordered something.<br />

This one creates a somewhat more significant change than is probably apparent at first. We’re now asking<br />

for the customer’s account number, which means that we have to bring the Customer table into play.<br />

In addition, we no longer need to build any kind of condition in — we’re asking for all customers (no<br />

restrictions), and we just want to know when each customer's first order date was.<br />

The query actually winds up being a bit simpler than the last one, and it looks like this:<br />

SELECT sc.AccountNumber,<br />

(SELECT Min(OrderDate)<br />

FROM Sales.SalesOrderHeader soh<br />

WHERE soh.CustomerID = sc.CustomerID)<br />

AS OrderDate<br />

FROM Sales.Customer sc;<br />

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

195

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

Saved successfully!

Ooh no, something went wrong!