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.

Note that whether you use a LEFT or a RIGHT JOIN doesn’t matter as long as the correct<br />

table or group of tables is on the corresponding side of the JOIN. For example,<br />

we could have run the preceding query using a RIGHT JOIN as long as we also<br />

switched which sides of the JOIN the Person and Employee tables were on. For<br />

example, this would have yielded exactly the same results:<br />

SELECT pp.BusinessEntityID, pp.FirstName, pp.LastName<br />

FROM HumanResources.Employee hre<br />

RIGHT OUTER JOIN Person.Person pp<br />

ON pp.BusinessEntityID = hre.BusinessEntityID<br />

WHERE hre.BusinessEntityID IS NULL<br />

When we take a look at even more advanced queries, we’ll run into a slightly more popular way of finding<br />

records that exist in one table without there being corresponding records in another table. Allow me to<br />

preface that by saying that using JOINs is usually our best bet in terms of performance. There are exceptions<br />

to the rule that we will cover as we come across them, but in general, the use of JOINs will be best<br />

when faced with multiple options.<br />

Dealing with More Complex OUTER JOINs<br />

Now we’re on to our second illustration and how to make use of it. This scenario is all about dealing<br />

with an OUTER JOIN mixed with some other JOIN (no matter what the variety).<br />

It is when combining an OUTER JOIN with other JOINs that the concept of sides becomes even more critical.<br />

What’s important to understand here is that everything to the “left” — or before — the JOIN in<br />

question will be treated just as if it were a single table for the purposes of inclusion or exclusion from the<br />

query. The same is true for everything to the “right” — or after — the JOIN. The frequent mistake here is<br />

to perform a LEFT OUTER JOIN early in the query and then use an INNER JOIN late in the query. The<br />

OUTER JOIN includes everything up to that point in the query, but the INNER JOIN may still create a<br />

situation where something is excluded! My guess is that you will, like most people (including me for<br />

a while), find this exceptionally confusing at first, so let’s see what we mean with some examples. Because<br />

none of the databases that come along with <strong>SQL</strong> <strong>Server</strong> has any good scenarios for demonstrating this,<br />

we’re going to have to create a database and sample data of our own.<br />

If you want to follow along with the examples, the example database called Chapter4DB can be created<br />

by running Chapter4DB.sql from the downloadable source code. Simply open the file in the Management<br />

Studio query window and execute it.<br />

Again, in order to utilize the next several examples, you must execute the<br />

Chapter4DB.sql script included in the downloadable code for this book.<br />

Chapter 4: JOINs<br />

What we are going to do is to build up a query step-by-step and watch what happens. The query we are<br />

looking for will return a vendor name and the address of that vendor. The example database only has a<br />

few records in it, so let’s start out by selecting all the choices from the central item of the query — the<br />

97

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

Saved successfully!

Ooh no, something went wrong!