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.

Notice that we did not use the INNER keyword in the query. That is because an INNER JOIN is the<br />

default JOIN type. Schools of thought vary on this, but I believe that because leaving the INNER keyword<br />

out has dominated the way code has been written for so long, it is almost more confusing to put<br />

it in — that’s why you won’t see me use it again in this book.<br />

OUTER JOINs<br />

This type of JOIN is something of the exception rather than the rule. This is definitely not because they<br />

don’t have their uses, but rather because:<br />

❑ We, more often than not, want the kind of exclusiveness that an inner join provides.<br />

❑ Many <strong>SQL</strong> writers learn inner joins and never go any further — they simply don’t understand<br />

the outer variety.<br />

❑ There are often other ways to accomplish the same thing.<br />

❑ They are often simply forgotten about as an option.<br />

Whereas inner joins are exclusive in nature, outer and, as we’ll see later in this chapter, full joins are<br />

inclusive. It’s a tragedy that people don’t get to know how to make use of outer joins because they make<br />

seemingly difficult questions simple. They can also often speed performance when used instead of<br />

nested subqueries (which we will look into in Chapter 7).<br />

Earlier in this chapter, we introduced the concept of a join having sides — a left and a right. The first<br />

named table is considered to be on the left and the second named table is considered to be on the right. With<br />

inner joins these are a passing thought at most, because both sides are always treated equally. With outer<br />

joins, however, understanding your left from your right is absolutely critical. When you look at it, it seems<br />

very simple because it is very simple, yet many query mistakes involving outer joins stem from not<br />

thinking through your left from your right.<br />

To learn how to construct outer joins correctly, we’re going to use two syntax illustrations. The first deals<br />

with the simple scenario of a two-table outer join. The second will deal with the more complex scenario<br />

of mixing outer joins with any other join.<br />

The Simple OUTER JOIN<br />

The first syntax situation is the easy part — most people get this part just fine:<br />

SELECT <br />

FROM <br />

[OUTER] JOIN <br />

ON <br />

Chapter 4: JOINs<br />

In the examples, you’ll find that I tend to use the full syntax — that is, I include the OUTER keyword<br />

(for example, LEFT OUTER JOIN). Note that the OUTER keyword is optional — you need only include<br />

the LEFT or RIGHT (for example, LEFT JOIN). In practice, I find that the OUTER keyword is rarely used.<br />

91

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

Saved successfully!

Ooh no, something went wrong!