What is the difference between UNION and UNIONALL?
Answer Posted / haroon nazir.s
UNION
The UNION command is used to select related information from
two tables, much like the JOIN command. However, when using
the UNION command all selected columns need to be of the
same data type. With UNION, only distinct values are selected.
UNION ALL
The UNION ALL command is equal to the UNION command, except
that UNION ALL selects all values.
The difference between Union and Union all is that Union all
will not eliminate duplicate rows, instead it just pulls all
rows from all tables fitting your query specifics and
combines them into a table.
A UNION statement effectively does a SELECT DISTINCT on the
results set. If you know that all the records returned are
unique from your union, use UNION ALL instead, it gives
faster results.
Example:
Table 1 : First,Second,Third,Fourth,Fifth
Table 2 : First,Second,Fifth,Sixth
Result Set:
UNION: First,Second,Third,Fourth,Fifth,Sixth (This will
remove duplicate values)
UNION ALL:
First,First,Second,Second,Third,Fourth,Fifth,Fifth,Sixth,Sixth
(This will repeat values)
| Is This Answer Correct ? | 5 Yes | 1 No |
Post New Answer View All Answers
Can I save my report as html, excel or word? : sql server management studio
What samples and sample databases are provided by microsoft?
How do you debug a procedure in sql server?
You want to use bids to deploy a report to a different server than the one you chose in the report wizard. How can you change the server url?
Can you force a query to use a specific index?
In which tcp/ip port does sql server run? Can it be changed?
What options are available to audit login activity? : sql server security
What is the purpose of forms?
What is the maximum rows in sql server table?
Does a specific recovery model need to be used for a replicated database? : sql server replication
How to see existing views in ms sql server?
Is it possible in sql table to have more than one foreign key?
Do you know what are the reporting services components?
What is the difference between executequery () and executeupdate ()?
What is the need for group functions in sql?