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 4: JOINs<br />

Try It Out FULL JOINs<br />

102<br />

Let’s just get right to it by looking back at our previous query from the section on OUTER JOINs:<br />

SELECT v.VendorName, a.Address<br />

FROM VendorAddress va<br />

JOIN Address a<br />

ON va.AddressID = a.AddressID<br />

RIGHT OUTER JOIN Vendors v<br />

ON v.VendorID = va.VendorID<br />

What we want to do here is take it a piece at a time again and add some fields to the SELECT list that will<br />

let us see what’s happening. First, we’ll take the first two tables using a FULL JOIN:<br />

SELECT a.Address, va.AddressID<br />

FROM VendorAddress va<br />

FULL JOIN Address a<br />

ON va.AddressID = a.AddressID<br />

As it happens, a FULL JOIN on this section doesn’t yield any more than a RIGHT JOIN would have:<br />

Address AddressID<br />

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

1234 Anywhere 1<br />

567 Main St. 3<br />

999 1st St. NULL<br />

1212 Smith Ave NULL<br />

364 Westin NULL<br />

(5 row(s) affected)<br />

But wait — there’s more! Now add the second JOIN:<br />

SELECT a.Address, va.AddressID, v.VendorID, v.VendorName<br />

FROM VendorAddress va<br />

FULL JOIN Address a<br />

ON va.AddressID = a.AddressID<br />

FULL JOIN Vendors v<br />

ON va.VendorID = v.VendorID<br />

Now we have everything:<br />

Address AddressID VendorID VendorName<br />

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

1234 Anywhere 1 1 Don’s Database Design Shop<br />

567 Main St. 3 2 Dave’s Data<br />

999 1st St. NULL NULL NULL<br />

1212 Smith Ave NULL NULL NULL<br />

364 Westin NULL NULL NULL<br />

NULL NULL 3 The <strong>SQL</strong> Sequel<br />

(6 row(s) affected)

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

Saved successfully!

Ooh no, something went wrong!