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

Among the topics we’ll be covering in this chapter are:<br />

❑ Nested subqueries<br />

❑ Correlated subqueries<br />

❑ Derived tables<br />

❑ Making use of the EXISTS operator<br />

❑ MERGE<br />

❑ Optimizing query performance<br />

We’ll see how, using subqueries, we can make the seemingly impossible completely possible, and how<br />

an odd tweak here and there can make a big difference in your query performance.<br />

What Is a Subquer y?<br />

188<br />

A subquery is a normal T-<strong>SQL</strong> query that is nested inside another query. Subqueries are created using<br />

parentheses when you have a SELECT statement that serves as the basis for either part of the data or the<br />

condition in another query.<br />

Subqueries are generally used to fill one of a few needs:<br />

❑ To break a query into a series of logical steps<br />

❑ To provide a listing to be the target of a WHERE clause together with [IN|EXISTS|ANY|ALL]<br />

❑ To provide a lookup driven by each individual record in a parent query<br />

Some subqueries are very easy to think of and build, but some are extremely complex — it usually<br />

depends on the complexity of the relationship between the inner (the sub) and outer (the top) queries.<br />

It’s also worth noting that most subqueries (but definitely not all) can also be written using a join. In<br />

places where you can use a join instead, the join is usually the preferable choice for a variety of reasons<br />

we will continue to explore over the remainder of the book.<br />

I once got into a rather lengthy debate (perhaps 20 or 30 e-mails flying back and forth, with examples,<br />

reasons, and so on over a few days) with a coworker over the joins versus subqueries issue.<br />

Traditional logic says to always use the join, and that was what I was pushing (due to experience rather<br />

than traditional logic — you’ve already seen several places in this book where I’ve pointed out how traditional<br />

thinking can be bogus). My coworker was pushing the notion that a subquery would actually<br />

cause less overhead — I decided to try it out.<br />

What I found was essentially (as you might expect) that we were both right in certain circumstances.<br />

We will explore these circumstances fully toward the end of the chapter after you have a bit more<br />

background.<br />

Now that we know what a subquery theoretically is, let’s look at some specific types and examples of<br />

subqueries.

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

Saved successfully!

Ooh no, something went wrong!