union vs union all in sql

union vs union all in sql oracle

The UNION operator is a set operator that combines result sets of two or more SELECT statements into a single result set.
There is a performance hit when using UNION instead of UNION ALL, since the database server must do additional work to remove the duplicate rows, but usually you do not want the duplicates (especially when developing reports).
To identify duplicates, records must be comparable types as well as compatible types. This will depend on the SQL system. For example the system may truncate all long text fields to make short text fields for comparison (MS Jet), or may refuse to compare binary fields (ORACLE)

UNION statement

The following illustrates the syntax of the UNION operator that combines the result sets of two queries:

In this statement, the name1 , mame2 and name3 must have the same number of columns presented in the same order. In addition, the data type of the corresponding column must be in the same data type group such as number or character.

UNION example

Result union statment

nametest

UNION ALL statement

UNION ALL example

Result union all statment

nametest
nametest
nametester

 

 

Reference :

Oracle UNION


https://stackoverflow.com/questions/49925/what-is-the-difference-between-union-and-union-all

Posted in SQL.