Re: Small SQL Puzzle
Posted in 1998
In article <34F552C8.163@mcmail.com>, Paul Watson <watson@mcmail.com> wrote:
>Consider the following
>
>select col1, count(*)
> from tab1
> group by 1
> having count(*) = 1
>union
>select col1, count(*)
> from tab2
> group by 1
> having count(*) = 1>
>Gives x rows
>
>Next
>
>select col1
> from tab1
>union
> select col1
> from tab2
>into temp table;>
>select col1, count(*)
>from temp
>having count(*) =1>
>gives y rows
>
>The obvious question is why doesn't x=y?
The first select is like
"list all the people who won one medal or have one child"
whereas the second is like
"list all the people who have any medals or any children"
- union means unique-union so the second query will get
*all* the values of col1 from tab1 and from tab2.
You aren't the "Sea Shepherd" chap at all, are you?
- Paul (not a spokesman)