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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 4: JOINs<br />

How It Works<br />

Unlike when we were nonspecific about what columns we wanted (when we just used the *), this time<br />

we were specific about what we wanted. Thus <strong>SQL</strong> <strong>Server</strong> knew exactly what to give us — the columns<br />

have come out in exactly the order that we’ve specified in our SELECT list. Indeed, even adding in an<br />

additional table, we were able to mix columns between all tables in the order desired.<br />

How an INNER JOIN Is Like a WHERE Clause<br />

88<br />

In the INNER JOINs that we’ve done so far, we’ve really been looking at the concepts that will work for<br />

any JOIN type — the column ordering and aliasing is exactly the same for any JOIN. The part that<br />

makes an INNER JOIN different from other JOINs is that it is an exclusive JOIN — that is, it excludes all<br />

records that don’t have a value in both tables (the first named, or left table, and the second named, or<br />

right table).<br />

Our first example of this was shown in our imaginary Films and Actors tables. Bogey was left out<br />

because he didn’t have a matching movie in the Films table. Let’s look at a real example or two to show<br />

how this works.<br />

While you probably haven’t realized it in the previous examples, we’ve already been working with the<br />

exclusionary nature of the INNER JOIN. You see, the BusinessEntity table has many, many more rows<br />

than the 290 or so that we’ve been working with. Indeed, our BusinessEntity table has information on<br />

virtually any individual our company works with. They can be individuals associated with a particular<br />

customer or vendor, or they can be employees. Let’s check this out by seeing how many rows exist in the<br />

BusinessEntity table:<br />

SELECT COUNT(*)<br />

FROM Person.BusinessEntity<br />

Run this and, assuming you haven’t added any new ones or deleted some, you should come up with<br />

approximately 20,777 rows; that’s a lot more rows than the 290 that our Employee-related queries have<br />

been showing us!<br />

So where did those other 20,487 rows go? As expected, they were excluded from the Employee query<br />

result set because there were no corresponding records in the Employee table. It is for this reason that an<br />

INNER JOIN is comparable to a WHERE clause. Just as the WHERE clause limits the rows returned to those<br />

that match the criteria specified, an INNER JOIN excludes rows because they have no corresponding<br />

match in the other table.<br />

Just for a little more proof and practice, let’s say we’ve been asked to produce a list of names associated<br />

with at least one customer and the account number of the customers they are associated with. Consider<br />

the following tables:

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

Saved successfully!

Ooh no, something went wrong!