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.

Now, look at the heart of what’s returned (you’ll see some one row(s) affecteds in there — just ignore<br />

them until you get to where the results of your query are visible):<br />

col2<br />

----<br />

AAA<br />

BBB<br />

CCC<br />

DDD<br />

EEE<br />

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

Divider Line-------------------------col2<br />

----<br />

AAA<br />

BBB<br />

CCC<br />

CCC<br />

DDD<br />

EEE<br />

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

The first result set returned was a simple UNION statement with no additional parameters. You can see<br />

that one row was eliminated. Even though we inserted “CCC” into both tables, only one makes an<br />

appearance since the duplicate record is eliminated by default.<br />

The second return changed things a bit. This time we used a UNION ALL and the ALL keyword ensured<br />

that we get every row back. As such, our eliminated row from the last query suddenly reappears.<br />

Summary<br />

Chapter 4: JOINs<br />

In an RDBMS, the data we want is quite frequently spread across more than one table. JOINs allow us to<br />

combine the data from multiple tables in a variety of ways:<br />

❑ Use an INNER JOIN when you want to exclude non-matching fields.<br />

❑ Use an OUTER JOIN when you want to retrieve matches wherever possible, but also want a<br />

fully inclusive data set on one side of the JOIN.<br />

❑ Use a FULL JOIN when you want to retrieve matches wherever possible, but also want a fully<br />

inclusive data set on both sides of the JOIN.<br />

❑ Use a CROSS JOIN when you want a Cartesian product based on the records in two tables. This<br />

is typically used in scientific environments and when you want to create test data.<br />

❑ Use a UNION when you want the combination of the result of a second query appended to the<br />

first query.<br />

111

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

Saved successfully!

Ooh no, something went wrong!