[vox-tech] SQL help - unions

David Hummel dhml at comcast.net
Wed Jul 21 01:36:45 PDT 2004


On Wed, Jul 21, 2004 at 12:00:39AM -0700, Jeff Newmiller wrote:
> 
> A headcount is not the same as a roster... neither Peter's nor
> Michael's version was counting anything.

I assumed what Peter was after was a list of unique SSN's for "race = 0"
students in Fall_2003 and Spring_2004.

> In fact, Michael's version is just wrong.  He uses an open join,

[ SELECT distinct SSN from Fall_2003, Spring_2004 where race = 0; ]

I think you mean an INNER or CROSS join (the cartesian product).

> which is different than a union join.

A UNION is not a join, it's a combination of results from separate
queries where the number and type of columns are the same.  You actually
can prevent eliminating duplicates with UNION ALL, but that's not what
you want in this case.

> The former concatenates the columns to give two distinct "SSN" columns
> and selects all possible pairings of the values in these columns, and
> the columns must be referred to with table names to distinguish them.

You are correct, and you will get an error due to the ambiguous "SSN"
column.

> Since he isn't constraining this open join, he ends up with all
> possible pairings of SSN values (if there are 200 in the fall and 300
> in the spring, then there are 60000 rows in his result).

Well not really because of the use of DISTINCT...

Maybe a NATURAL join would work, but the UNION method is the most
straightforward.

-David


More information about the vox-tech mailing list