Combining union and union all

  • R.P.Rozema (3/8/2012)


    To finalize this: My explanation is wrong and my question + answer was correct only by luck.

    The proper explanation has been given in this thread. To be sure I am getting it now I'll try to summarize it:

    Union queries are interpreted left to right. If "union all" is followed by "union", the "union all" will return duplicates, but these will be filtered by the following "union". Other way around, if "union" is followed by "union all", any duplicates from the first 2 statements are filtered, but new duplicates may be introduced by the following "union all".

    Parenthesis can be used to override the left-to-right evaluation.

    An illustration can be given by putting more rows in the test tables:

    create table #t1 (col int not null);

    create table #t2 (col int not null);

    create table #t3 (col int not null);

    insert #t1 (col) values(1), (1);

    insert #t2 (col) values(2), (2);

    insert #t3 (col) values(3), (3);

    select col from #t1

    UNION

    select col from #t2

    UNION ALL

    select col from #t3;

    select col from #t1

    UNION ALL

    select col from #t2

    UNION

    select col from #t3;

    And now the results are:

    col

    -----------

    1

    2

    3

    3

    (4 row(s) affected)

    col

    -----------

    1

    2

    3

    (3 row(s) affected)

    Seems like I was the first to learn something from my own question :).

    Thanks for all the feedback!

    OK!

  • good question!


    [font="Times New Roman"]rfr.ferrari[/font]
    DBA - SQL Server 2008
    MCITP | MCTS

    remember is live or suffer twice!
    the period you fastest growing is the most difficult period of your life!
  • R.P.Rozema (3/8/2012)


    To finalize this: My explanation is wrong and my question + answer was correct only by luck.

    ...

    And why is it a great question?

    Why UNION (ALL) is basic but very confusing and anyone can be tricked by it.

    Also it remember us to be meticulous with precedence.

    Thanks Rozema.

  • Excellent question. Glad you could learn something too! Thanks for your efforts.

    [font="Verdana"]Please don't go. The drones need you. They look up to you.[/font]
    Connect to me on LinkedIn

  • Nice.

    (from next time rather converting to image, it will be better to paste the exact SQL)

    ww; Raghu
    --
    The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart.

  • Raghavendra Mudugal (3/8/2012)


    (from next time rather converting to image, it will be better to paste the exact SQL)

    Actually no. The code was intentionally presented as a picture for 2 reasons:

    1 - cheaters will have to type the entire challenge when they try to find the correct answer by simply running it, and

    2 - presented as a picture you'll have my syntax highlighting to aid you in reading the code correctly.



    Posting Data Etiquette - Jeff Moden[/url]
    Posting Performance Based Questions - Gail Shaw[/url]
    Hidden RBAR - Jeff Moden[/url]
    Cross Tabs and Pivots - Jeff Moden[/url]
    Catch-all queries - Gail Shaw[/url]


    If you don't have time to do it right, when will you have time to do it over?

  • R.P.Rozema (3/8/2012)


    Raghavendra Mudugal (3/8/2012)


    (from next time rather converting to image, it will be better to paste the exact SQL)

    Actually no. The code was intentionally presented as a picture for 2 reasons:

    1 - cheaters will have to type the entire challenge when they try to find the correct answer by simply running it, and

    2 - presented as a picture you'll have my syntax highlighting to aid you in reading the code correctly.

    I will type the complete code only if the SQL code has interesting thing to learn, just inserting in to temp and using couple of unions - either typing or copy/pasting will not make any sense - as long as the complete focus is on the UNION/ALL feature.

    Typing is always fun when something to "type" interesting.

    anyways its your question and its your method of posting... 😉

    ww; Raghu
    --
    The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart.

  • Nice back to basics question. Keep them coming!!!

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • create table #t1( col int not null);

    insert #t1(col) values(1),(1),(2),(3),(3)

    select col from #t1

    union

    select col from #t1

    union all

    select col from #t1

    select col from #t1

    union all

    select col from #t1

    union

    select col from #t1

    select col from #t1

    union all

    (

    select col from #t1

    union

    select col from #t1

    )

    drop table #t1

    This is why I got the question wrong, I only used one table, I didn't think about order of table.

    Order of table is exactly why this union is acting the way it did for the answer.

    Great challenging post! 🙂

  • Great question. The Union will eliminate the duplicates as has been demonstrated. Thanks.

  • Very nice. Thanks for the question!

    Chris Umbaugh
    Data Warehouse / Business Intelligence Consultant
    twitter @ToledoSQL

  • Nice easy question. Still got me to go dig into BOL to make sure I had the right answer before I clicked submit though, so thank you!

  • Thank you for the question. It also made me dig into BOL and scratch my head understanding UNIONs and precedence.

    "El" Jerry.

    "El" Jerry.

    "A watt of Ottawa" - Gerardo Galvan

    To better understand your help request, please follow these best practices.[/url]

  • Thanks for the question.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Thanks for a good question and a great discussion.

    Once again, the lesson is to pay attention to all the details....

    Rob Schripsema
    Propack, Inc.

Viewing 15 posts - 16 through 30 (of 49 total)

You must be logged in to reply to this topic. Login to reply