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

4 JOINs Feel like a seasoned professional yet? Let me dash that feeling right away (just kidding)! While we now have the basic statements under our belt, they are only a small part of the bigger picture of the statements we will run. To put it simply, there is often not that much you can do with just one table — especially in a highly normalized database. A normalized database is one where the data has been broken out from larger tables into many smaller tables for the purpose of eliminating repeating data, saving space, improving performance, and increasing data integrity. It’s great stuff and vital to relational databases; however, it also means that you wind up getting your data from here, there, and everywhere. We will be looking into the concepts of normalization extensively in Chapter 8. For now, though, just keep in mind that the more normalized your database is, the more likely that you’re going to have to join multiple tables together in order to get all the data you want. In this chapter, I’m going to introduce you to the process of combining tables into one result set by using the various forms of the JOIN clause. These will include: ❑ INNER JOIN ❑ OUTER JOIN (both LEFT and RIGHT) ❑ FULL JOIN ❑ CROSS JOIN We’ll also learn that there is more than one syntax available to use for joins, and that one particular syntax is the right choice. In addition, we’ll take a look at the UNION operator, which allows us to combine the results of two queries into one. JOINs When we are operating in a normalized environment, we frequently run into situations in which not all of the information that we want is in one table. In other cases, all the information we want

4<br />

JOINs<br />

Feel like a seasoned professional yet? Let me dash that feeling right away (just kidding)! While<br />

we now have the basic statements under our belt, they are only a small part of the bigger picture<br />

of the statements we will run. To put it simply, there is often not that much you can do with just<br />

one table — especially in a highly normalized database.<br />

A normalized database is one where the data has been broken out from larger tables into many<br />

smaller tables for the purpose of eliminating repeating data, saving space, improving performance,<br />

and increasing data integrity. It’s great stuff and vital to relational databases; however, it also<br />

means that you wind up getting your data from here, there, and everywhere.<br />

We will be looking into the concepts of normalization extensively in Chapter 8. For now, though,<br />

just keep in mind that the more normalized your database is, the more likely that you’re going to<br />

have to join multiple tables together in order to get all the data you want.<br />

In this chapter, I’m going to introduce you to the process of combining tables into one result set by<br />

using the various forms of the JOIN clause. These will include:<br />

❑ INNER JOIN<br />

❑ OUTER JOIN (both LEFT and RIGHT)<br />

❑ FULL JOIN<br />

❑ CROSS JOIN<br />

We’ll also learn that there is more than one syntax available to use for joins, and that one particular<br />

syntax is the right choice. In addition, we’ll take a look at the UNION operator, which allows us to<br />

combine the results of two queries into one.<br />

JOINs<br />

When we are operating in a normalized environment, we frequently run into situations in which<br />

not all of the information that we want is in one table. In other cases, all the information we want

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

Saved successfully!

Ooh no, something went wrong!