Interview Question of the Week #017 – Performance Comparison of Union vs Union All

An email told me that a prestigious organization asked a candidate to choose between UNION and UNION ALL with performance as the top requirement. My first reaction was that this is an apples-and-oranges question: the two operators return different answers when duplicates exist.

Two apple collections show keeping every apple compared with selecting distinct ones

Question: Which would you choose for performance, UNION or UNION ALL?

Answer: Choose the required result first. UNION ALL appends every row from both inputs. UNION removes duplicate rows from the combined result. If duplicates are allowed, UNION ALL usually avoids the extra deduplication work. If duplicates must be removed, replacing UNION with UNION ALL just to make a query faster gives the wrong answer.

SELECT 'apple' AS Fruit
UNION
SELECT 'apple';       -- one row

SELECT 'apple' AS Fruit
UNION ALL
SELECT 'apple';       -- two rows

Both branches need the same number of columns in corresponding positions and compatible data types. UNION stacks rows; it is not a JOIN between columns. Once you know which result is right, inspect the actual plan and workload before claiming a particular elapsed-time difference.

My earlier articles include a performance comparison and why these are different operations. The interview answer is short: semantics first, speed second.

Original supporting examples

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

SQL Union clause
Previous Post
Interview Question of the Week #016 – How to Take Database Offline
Next Post
Interview Question of the Week #018 – Script to Remove Special Characters – Script to Parse Alpha Numerics

Related Posts

7 Comments. Leave new

  • If you have option to use both of them, doesn’t that mean that there are no duplicates? Otherwise it’s really not an option to use union all. Also to my mind they are very similar operators and developers don’t understand why / when to use union all.

    Reply
  • It should be noted that union and union all can not be used to combine sequence values. Refer this post for more information

    Reply
  • ScottPletcher
    April 27, 2015 7:53 pm

    That’s a strange non-answer to the q. Given that they did ask the q, the answer is UNION ALL, since it does not require a sort and duplicate removal, as UNION does.

    Reply
    • That’s what I said
      “You can’t compare their performance as they do an absolutely different task.”

      Reply
  • twoknightsthenight
    May 3, 2015 5:55 am

    You may also want to look at Kenneth Fisher’s entry in the below URL. Whomever is asking the question may have used this for a reference.

    I realize it’s a bit out of context but…

    “So what does that mean for you? Unless you actually need to use UNION (Ie you need to get rid of duplicates) then you want to use UNION ALL as it’s the much cheaper and faster option.

    There are a couple of exceptions. If you are doing a UNION in an EXISTS clause then SQL knows enough that it doesn’t bother with the sort and the execution times are the same. Also if you are already sorting the output (using an ORDER BY) then most of the cost is already taken care of.”

    I agree it’s a bad question but I’d love to know how they expected it to be answered. I frequently have run into interviewers who neither understand the question nor the answer. They just read from a script.

    Reply
  • Trick interview question for sure. I gotta hope at least that’s what they’re thinking. Otherwise, they aren’t very knowledgeable in UNION queries.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.