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.

Dave’s Data 1212 Smith Ave<br />

Dave’s Data 364 Westin<br />

The <strong>SQL</strong> Sequel 1234 Anywhere<br />

The <strong>SQL</strong> Sequel 567 Main St.<br />

The <strong>SQL</strong> Sequel 999 1st St.<br />

The <strong>SQL</strong> Sequel 1212 Smith Ave<br />

The <strong>SQL</strong> Sequel 364 Westin<br />

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

This is supported across all versions and across most of the database management systems.<br />

The UNION<br />

OK, enough with all the “old syntax” versus “new syntax” stuff. Now we’re into something that’s the<br />

same regardless of what other join syntax you prefer — the UNION operator. UNION is a special operator<br />

we can use to cause two or more queries to generate one result set.<br />

A UNION isn’t really a JOIN, like the previous options we’ve been looking at — instead it’s more of an<br />

appending of the data from one query right onto the end of another query (functionally, it works a little<br />

differently than this, but this is the easiest way to look at the concept). Where a JOIN combined information<br />

horizontally (adding more columns), a UNION combines data vertically (adding more rows), as illustrated<br />

in Figure 4-1.<br />

When dealing with queries that use a UNION, there are just a few key points:<br />

Chapter 4: JOINs<br />

❑ All the UNIONed queries must have the same number of columns in the SELECT list. If your first<br />

query has three columns in the SELECT list, then the second (and any subsequent queries being<br />

UNIONed) must also have three columns. If the first has five, then the second must have five, too.<br />

Regardless of how many columns are in the first query, there must be the same number in the<br />

subsequent query(s).<br />

❑ The headings returned for the combined result set will be taken only from the first of the<br />

queries. If your first query has a SELECT list that looks like SELECT Col1, Col2 AS Second,<br />

Col3 FROM..., then regardless of how your columns are named or aliased in the subsequent<br />

queries, the headings on the columns returned from the UNION will be Col1, Second, and Col3<br />

respectively.<br />

❑ The data types of each column in a query must be implicitly compatible with the data type in<br />

the same relative column in the other queries. Note that I’m not saying they have to be the same<br />

data type — they just have to be implicitly convertible (a conversion table that shows implicit<br />

versus explicit conversions can be found in Figure 1-3 of Chapter 1). If the second column in<br />

the first query were of type char(20), then it would be fine if the second column in the second<br />

query were varchar(50). However, because things are based on the first query, any rows<br />

longer than 20 would be truncated for data from the second result set.<br />

❑ Unlike non-UNION queries, the default return option for UNIONs is DISTINCT rather than ALL.<br />

This can really be confusing to people. In your other queries, all rows were returned regardless<br />

of whether they were duplicated with another row or not, but the results of a UNION do not work<br />

that way. Unless you use the ALL keyword in your query, only one of any repeating rows will be<br />

returned.<br />

107

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

Saved successfully!

Ooh no, something went wrong!