Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4991

Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4993
Question 29: What is the result of the following query? – MySQL Question of the Day

Skip to content

By urs in mysql questions

 

create table utst1(a int);
insert into utst1 values (1),(1),(2),(2),(3),(3);
create table utst2(b int);
 
select * from utst1 union select * from utst2 order by 1;

 
Possible results:
 

a)
+------+
| a    |
+------+
|    1 |
|    1 |
|    2 |
|    2 |
|    3 |
|    3 |
+------+
6 rows in set (0.00 sec)
 
b)
+------+
| a    |
+------+
|    1 |
|    2 |
|    3 |
+------+
3 rows in set (0.01 sec)

 
[ Querying for Data (15%) - Using UNION ]
 

Tags: , ,

Comment Feed

One Response


  1.  
    Answer:
     
    b
     
    UNION will always remove duplicates from the resultset, even if the duplicates aren’t the result of the UNION. (like in the example) If you need to have all rows returned, use UNION ALL.
     
    a is actually the output of:
    select * from utst1 union all select * from utst2;
     

You must be logged in to post a comment.