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

96<br />

Let’s use this notion of being able to identify non-matching records to locate some of the missing records<br />

from one of our earlier INNER JOINs. Remember these two queries, which you ran against Adventure-<br />

Works<strong>2008</strong>?<br />

SELECT pbe.BusinessEntityID, hre.JobTitle, pp.FirstName, pp.LastName<br />

FROM Person.BusinessEntity pbe<br />

INNER JOIN HumanResources.Employee hre<br />

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

INNER JOIN Person.Person pp<br />

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

WHERE hre.BusinessEntityID < 4<br />

And . . .<br />

SELECT COUNT(*)<br />

FROM Person.BusinessEntity<br />

The first was one of our queries where we explored the INNER JOIN. We discovered by running the<br />

second query that the first had excluded (by design) some rows. Now let’s identify the excluded rows by<br />

using an OUTER JOIN.<br />

We know from our SELECT COUNT(*) query that our first query is missing thousands of records from<br />

the BusinessEntity table. (It could conceivably be missing records from the Employee table, but we’re<br />

not interested in that at the moment.) The implication is that there are records in the BusinessEntity<br />

table that do not have corresponding Employee records. This makes sense, of course, because we know<br />

that some of our persons are customers or vendor contacts. While our manager’s first question was about<br />

all the employee contact information, it would be very common to ask just the opposite: “Which persons<br />

are not employees?” That question is answered with the same result obtained by asking, “Which records<br />

exist in Person that don’t have corresponding records in the Employee table?” The solution has the<br />

same structure as the query to find special offers that aren’t associated with any products:<br />

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

FROM Person.Person pp<br />

LEFT OUTER JOIN HumanResources.Employee hre<br />

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

WHERE hre.BusinessEntityID IS NULL<br />

Just that quickly we have a list of contacts that is somewhat cleaned up to contain just customers:<br />

BusinessEntityID FirstName LastName<br />

---------------- --------------------- --------------------------------<br />

293 Catherine Abel<br />

295 Kim Abercrombie<br />

2170 Kim Abercrombie<br />

…<br />

…<br />

2088 Judy Zugelder<br />

12079 Jake Zukowski<br />

2089 Michael Zwilling<br />

(19682 row(s) affected)

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

Saved successfully!

Ooh no, something went wrong!