Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another SQL Question: Union/Union All

 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, another practice question courtesy of LearnKey for the SQL Expert Exam



The question is "how many rows are returned?"

My logic was 2. One for the 'NAME' (JOE) in the top, and one for 'PERSON' (MARY) in the bottom.

The correct answer is 3... "MARY" in the first inline view, and "MARY" and "LENNY" in the second.

Why?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think there's only one at the bottom ?
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay... first query...
takes NAME and PERSON.... selects NAME (which is Joe)

Second query...
takes PERSON and CLERK... selects PERSON (which is Mary)

then the union between the two is NAME (Joe) and PERSON (Mary)

This could be totally off, but I'm not sure how it's supposed to work.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The alias is just the name of the resulting column which will finally be used in the result, it's not used during the union.

first query...
takes JOE and MARY, removes JOE.... The resulting column called NAME has one result : MARY

Second query...
takes MARY and LENNY.... The resulting column called PERSON has two results : MARY and LENNY.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You know Christophe, I look at this stuff and try to figure it out over and over...

Then I come here and you totally make it look easy. I wonder why I didn't see it before.

Thanks so much. Again.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm glad it helps
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic