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
What is user-defined function?
How to populate a table in sql server?
How do you drop an index?
How do I find the default sql server instance?
Describe in brief authentication modes in sql server.
What types of replication are supported in sql server?
Working with TLogs
Where the sql logs gets stored? : sql server database administration
Why we need sql server?
What is dml command?
What is scan table/view and seek table/view when its occurs? : sql server database administration
What is the Difference Between Primary and Foreign Key?
How to store and query spatial data?
What is a database table?
What is the use of builtinadministrators group in sql server? : sql server security