Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

cdn.s3techtraining.com
from cdn.s3techtraining.com More from this publisher
17.06.2013 Views

Chapter 4: JOINs How It Works Unlike when we were nonspecific about what columns we wanted (when we just used the *), this time we were specific about what we wanted. Thus SQL Server knew exactly what to give us — the columns have come out in exactly the order that we’ve specified in our SELECT list. Indeed, even adding in an additional table, we were able to mix columns between all tables in the order desired. How an INNER JOIN Is Like a WHERE Clause 88 In the INNER JOINs that we’ve done so far, we’ve really been looking at the concepts that will work for any JOIN type — the column ordering and aliasing is exactly the same for any JOIN. The part that makes an INNER JOIN different from other JOINs is that it is an exclusive JOIN — that is, it excludes all records that don’t have a value in both tables (the first named, or left table, and the second named, or right table). Our first example of this was shown in our imaginary Films and Actors tables. Bogey was left out because he didn’t have a matching movie in the Films table. Let’s look at a real example or two to show how this works. While you probably haven’t realized it in the previous examples, we’ve already been working with the exclusionary nature of the INNER JOIN. You see, the BusinessEntity table has many, many more rows than the 290 or so that we’ve been working with. Indeed, our BusinessEntity table has information on virtually any individual our company works with. They can be individuals associated with a particular customer or vendor, or they can be employees. Let’s check this out by seeing how many rows exist in the BusinessEntity table: SELECT COUNT(*) FROM Person.BusinessEntity Run this and, assuming you haven’t added any new ones or deleted some, you should come up with approximately 20,777 rows; that’s a lot more rows than the 290 that our Employee-related queries have been showing us! So where did those other 20,487 rows go? As expected, they were excluded from the Employee query result set because there were no corresponding records in the Employee table. It is for this reason that an INNER JOIN is comparable to a WHERE clause. Just as the WHERE clause limits the rows returned to those that match the criteria specified, an INNER JOIN excludes rows because they have no corresponding match in the other table. Just for a little more proof and practice, let’s say we’ve been asked to produce a list of names associated with at least one customer and the account number of the customers they are associated with. Consider the following tables:

Person.Person Sales.Customer BusinessEntityID CustomerID PersonType PersonID NameStyle StoreID Title TerritoryID FirstName AccountNumber MiddleName rowguid LastName ModifiedDate Suffix EmailPromotion AdditionalContactInfo Demographics rowguid ModifiedDate Try coming up with this query on your own for a few minutes, then we’ll dissect it a piece at a time. The first thing to do is to figure out what data we need to return. The question calls for two different pieces of information to be returned: the person’s name and the account number(s) of the customer they are associated with. The contact’s name is available (in parts) from the Person.Person table. The customer’s account number is available in the Sales.Customer table, so we can write the first part of our SELECT statement. For brevity’s sake, we’ll just worry about the first and last name of the contact: SELECT LastName + ‘, ‘ + FirstName AS Name, AccountNumber Chapter 4: JOINs As in many development languages, the + operator can be used for concatenation of strings as well as the addition of numbers. In this case, we are just connecting the last name to the first name with a comma separator in between. What we need now is something to join the two tables on, and that’s where we run into our first problem — there doesn’t appear to be one. The tables don’t seem to have anything in common on which we can base our JOIN – fortunately, looks can be deceiving. 89

Person.Person Sales.Customer<br />

BusinessEntityID CustomerID<br />

PersonType PersonID<br />

NameStyle StoreID<br />

Title TerritoryID<br />

FirstName AccountNumber<br />

MiddleName rowguid<br />

LastName ModifiedDate<br />

Suffix<br />

EmailPromotion<br />

AdditionalContactInfo<br />

Demographics<br />

rowguid<br />

ModifiedDate<br />

Try coming up with this query on your own for a few minutes, then we’ll dissect it a piece at a time.<br />

The first thing to do is to figure out what data we need to return. The question calls for two different<br />

pieces of information to be returned: the person’s name and the account number(s) of the customer they<br />

are associated with. The contact’s name is available (in parts) from the Person.Person table. The customer’s<br />

account number is available in the Sales.Customer table, so we can write the first part of our<br />

SELECT statement. For brevity’s sake, we’ll just worry about the first and last name of the contact:<br />

SELECT LastName + ‘, ‘ + FirstName AS Name, AccountNumber<br />

Chapter 4: JOINs<br />

As in many development languages, the + operator can be used for concatenation of strings as well as<br />

the addition of numbers. In this case, we are just connecting the last name to the first name with a<br />

comma separator in between.<br />

What we need now is something to join the two tables on, and that’s where we run into our first problem<br />

— there doesn’t appear to be one. The tables don’t seem to have anything in common on which we<br />

can base our JOIN – fortunately, looks can be deceiving.<br />

89

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

Saved successfully!

Ooh no, something went wrong!