Friday, January 18, 2019

Difference Between Union and Union All

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.

The following points need to be considered when using the UNION operator:

The number of columns and sequence of columns must be the same in all queries
The data types must be compatible.

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.

Table 1

First
Second
Third
Fourth
Fifth

Second Table

First
Third
Fifth


Select * from Table1 Union All Select * from Table2

First
Second
Third
Fourth
Fifth
First
Third
Fifth


Select * from Table1 Union Select * from Table2

First
Third
Fifth
Second
Fourth

No comments:

Followers

Link