what is the difference between UNION AND UNIONALL

Answer Posted / vinod singh kushwah

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.

Note: With UNION, only distinct values are selected.

SQL Statement 1
UNION
SQL Statement 2

Employees_Norway:

E_ID E_Name
01 Hansen, Ola
02 Svendson, Tove
03 Svendson, Stephen
04 Pettersen, Kari

Employees_USA:

E_ID E_Name
01 Turner, Sally
02 Kent, Clark
03 Svendson, Stephen
04 Scott, Stephen


------------------------------------------------------------
--------------------

Using the UNION Command
Example
List all different employee names in Norway and USA:

SELECT E_Name FROM Employees_Norway
UNION
SELECT E_Name FROM Employees_USA

Result

E_Name
Hansen, Ola
Svendson, Tove
Svendson, Stephen
Pettersen, Kari
Turner, Sally
Kent, Clark
Scott, Stephen

Note: This command cannot be used to list all employees in
Norway and USA. In the example above we have two employees
with equal names, and only one of them is listed. The UNION
command only selects distinct values.


UNION ALL
The UNION ALL command is equal to the UNION command, except
that UNION ALL selects all values.

SQL Statement 1
UNION ALL
SQL Statement 2


------------------------------------------------------------
--------------------

Using the UNION ALL Command
Example
List all employees in Norway and USA:

SELECT E_Name FROM Employees_Norway
UNION ALL
SELECT E_Name FROM Employees_USA

Result

E_Name
Hansen, Ola
Svendson, Tove
Svendson, Stephen
Pettersen, Kari
Turner, Sally
Kent, Clark
Svendson, Stephen
Scott, Stephen

Is This Answer Correct ?    20 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is an oracle cursor variable?

609


What is the Tune Query

1845


Explain oracle data types with examples?

576


How do I reset a sequence in oracle?

596


How to import one table back from a dump file?

595






Explain the importance of .pll extension in oracle?

555


Select all the employees who were hired in last 2 years and who works in dept where max managers are working.

2453


If a parameter is used in a query without being previously defined, what diff. exist betw. report 2.0 and 2.5 when the query is applied ?

1798


How to start instance with a minimal initialization parameter file?

535


How to define a record variable to store a table row?

577


What is the usage of control file in oracle?

609


What is a cursor in oracle?

650


Explain what are the advantages of views?

613


What is a view and how is it different from a table?

572


What is Undo Management Advisor in Oracle?

613