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.

The question you should be asking now is, “Which way is best?” Quite often in <strong>SQL</strong>, there are several<br />

ways of executing the query without one having any significant advantage over the other — this is not<br />

one of those times.<br />

I would most definitely steer you to the second of the two solutions.<br />

The reason has to do with navigating as quickly as possible to your data. If you keep adding OUTER<br />

JOINs not because of what’s happening with the current table you’re trying to add in, but because you’re<br />

trying to carry through an earlier JOIN result, you are much more likely to include something you don’t<br />

intend, or to make some sort of mistake in your overall logic. The second solution addresses this by<br />

using only the OUTER JOIN where necessary — just once. You can’t always create a situation where the<br />

JOINs can be moved around to this extent, but you often can.<br />

I can’t stress enough how often I see errors with JOIN order. It is one of those areas that just seems to<br />

give developers fits. Time after time I get called in to look over a query that someone has spent hours<br />

verifying each section of, and it seems that at least half the time I get asked whether I know about this<br />

<strong>SQL</strong> <strong>Server</strong> “bug.” The bug isn’t in <strong>SQL</strong> <strong>Server</strong> — it’s with the developer. If you take anything away<br />

from this section, I hope it is that JOIN order is one of the first places to look for errors when the results<br />

aren’t coming up as you expect.<br />

Seeing Both Sides with FULL JOINs<br />

Chapter 4: JOINs<br />

The rule of thumb is to get all of the INNER JOINs you can out of the way first; you<br />

will then find yourself using the minimum number of OUTER JOINs and decreasing<br />

the number of errors in your data.<br />

Like many things in <strong>SQL</strong>, a FULL JOIN (also known as a FULL OUTER JOIN) is basically what it sounds<br />

like — it is a matching up of data on both sides of the JOIN with everything included, no matter what<br />

side of the JOIN it is on.<br />

FULL JOINs seem really cool when you learn them and then almost never get used. You’ll find an honest<br />

politician more often than you’ll find a FULL JOIN in use. Their main purpose in life is to look at the<br />

complete relationship between data without giving preference to one side or the other. You want to<br />

know about every record on both sides of the equation — with nothing left out.<br />

A FULL JOIN is perhaps best described as what you would get if you could do a LEFT JOIN and a<br />

RIGHT JOIN in the same JOIN. You get all the records that match, based on the JOIN field(s). You also<br />

get any records that exist only on the left side, with NULLs being returned for columns from the right<br />

side. Finally, you get any records that exist only on the right side, with NULLs being returned for columns<br />

from the left side. Note that, when I say finally, I don’t mean to imply that they’ll be last in the query. The<br />

result order you get will (unless you use an ORDER BY clause) depend entirely on what <strong>SQL</strong> <strong>Server</strong><br />

thinks is the least costly way to retrieve your records.<br />

101

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

Saved successfully!

Ooh no, something went wrong!