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


Please Help Members By Posting Answers For Below Questions

What is difference between foreign key and unique key?

751


Why do we use sql limitations? Which constraints can we use while making a database in sql?

742


What is read uncmmited?

788


what's the information that can be stored inside a bit column? : Sql server database administration

723


How do I completely remove sql server instance?

776






When to use Inner join & when to use subquery?

750


what are the steps you will take to improve performance of a poor performing query? : Sql server database administration

705


What do you understand by replication in sql server?

746


Mention the command used to rename the database.

725


What is buffer cash in sql server?

813


How to concatenate two strings in SQL Server.

783


What are binary string data types in ms sql server?

817


Indexes are updated automatically is the full-text index also updated automatically?

793


Explain the different index configurations a table can have?

683


What are the database objects? : SQL Server Architecture

701