Thursday, September 25, 2014

SET OPERATORS (I) - UNION and duplicates

It is well known that the UNION set operator removes duplicates.
One can see that in the sql server official doc.

But there is a point related with duplicates removal that it is not explained so well.
As I have seen sometimes, one could wrongly understand that given two sets A and B, the UNION of A and B will remove from the result ONLY the elements in B that are contained in A as well.

Let's see how UNION operator deals with duplicates actually with some examples. First creating the tables and their content for the example.



These are the contents of ItemsA and ItemsB
ItemsA:


ItemID ItemModel                 ItemColor
------ ------------------------- -------------------------
1      MP3 Player                red
2      MP3 Player                red
3      MP3 Player                blue
4      Smartphone                yellow
5      Smartphone                blue




ItemsB:


ItemID ItemModel                 ItemColor
------ ------------------------- -------------------------
15     MP3 Player                green
16     MP3 Player                orange
17     Smartphone                green
18     Smartphone                yellow


Now we have the ingredients ready. In the following we can see a simple UNION statement between ItemsA and ItemsB. NOTE: The ID columns are not present because we want to see how the UNION manages the duplicates.





Then, what result do we expect if we think the UNION of A and B will remove from the result ONLY the elements in B that are contained in A? This one

ItemModel                 ItemColor
------------------------- -------------------------
MP3 Player                red      /* from itemsA */
MP3 Player                red      /* from itemsA */
MP3 Player                blue     /* from itemsA */
Smartphone                yellow   /* from itemsA */
Smartphone                blue     /* from itemsA */
MP3 Player                green    /* from itemsB */
MP3 Player                orange   /* from itemsB */
Smartphone                green    /* from itemsB */



One could imagine the only row that has been removed from the result is
18     Smartphone                yellow
from ItemsB, because there was already a yellow Smartphone in Items A.
Let's see which one is the true result of the UNION of ItemsA and ItemsB.


ItemModel                 ItemColor
------------------------- -------------------------
MP3 Player                blue    /* from itemsA */
MP3 Player                green   /* from itemsA */
MP3 Player                orange  /* from itemsB */
MP3 Player                red     /* from itemsA */
Smartphone                blue    /* from itemsB */
Smartphone                green   /* from itemsB */
Smartphone                yellow  /* from itemsA or itemsB */

A different result is shown. 
First of all, a yellow smartphone has been removed, as it had a duplicate. But the important thing that explains the behavior is that we have lost one red MP3 from itemsA. Why? Because the UNION set operator removes all the duplicates from the result of merging all the elements of the source tables not taking care whether they are from the same source of data.
The conclusion to bear in mind working with UNION is that UNION first puts all the elements together and then removes duplicate elements from the final set.

To finalize, what if we want what we possibly understood wrongly? How can we keep all the elements of ItemsA and all the elements of ItemsB that are not already present in ItemsA? 
There are several ways to do it, using operators as IN or EXISTS. I have done a query that firstly removes the elements from ItemsB that are present in ItemsA (with a LEFT JOIN) and secondly making a UNION ALL (UNION without remove any duplicates) with ItemsA. Here it is:




And this is the result:
ItemModel                 ItemColor
------------------------- -------------------------
MP3 Player                red
MP3 Player                red
MP3 Player                blue
Smartphone                yellow
Smartphone                blue
MP3 Player                green
MP3 Player                orange
Smartphone                green



Regards.

Pablo

No comments:

Post a Comment