what is the difference between union and union all

Answers were Sorted based on User's Feedback



what is the difference between union and union all..

Answer / suresh

union is used to select distinct values from two tables
where as union all is used to select all values including
duplicates from the tables

Is This Answer Correct ?    175 Yes 12 No

what is the difference between union and union all..

Answer / a.jyothsna

Suresh,
you are exactly correct

Is This Answer Correct ?    53 Yes 11 No

what is the difference between union and union all..

Answer / ajith

A UNION statement eliminates duplicate rows;
A UNION ALL statement includes duplicate rows.

Is This Answer Correct ?    37 Yes 4 No

what is the difference between union and union all..

Answer / gaurav arora

The answer-1 is very much correct. Let me clear it with
example:

Union vs. Union All
In simple we can say that
1. union is used to select distinct values from two
tables,where as union all is used to select all values
including duplicates from the tables.
2. The UNION operator allows you to combine the results of
two or more SELECT statements into a single result set. The
result sets combined using UNION must all have the same
structure. They must have the same number of columns, and
the corresponding result set columns must have compatible
data types.By default, the UNION operator removes duplicate
rows from the result set. If you use UNION ALL, all rows
are included in the results and duplicates are not removed.

Lets consider following examples:

1. UNION
Select * from dbo.checkDuplicate
Union --it will leave the duplicate rows
Select * from dbo.checkDuplicate

The above querry will retrieve all rows from checkduplicate
table except duplicate entries.

2. UNION ALL
Select * from dbo.checkDuplicate
Union --it will select all rows including duplicates
Select * from dbo.checkDuplicate

The above querry will select all rows from checkduplicate
table including duplicate entries.

Note: One can count the number of rows using following
statement:

SELECT rows FROM sysindexes WHERE id = OBJECT_ID
('checkDuplicate') AND indid < 2

To get above used table please run following querry:
CREATE TABLE dbo.checkDuplicate
(
[ID] [int] ,
[FirstName] [varchar](25),
[LastName] [varchar](25)
) ON [PRIMARY]

INSERT INTO dbo.checkDuplicate VALUES(1, 'Gaurav','Arora')
INSERT INTO dbo.checkDuplicate VALUES(2, 'Shuby','Arora')
INSERT INTO dbo.checkDuplicate VALUES(3, 'Amit','Gupta')
INSERT INTO dbo.checkDuplicate VALUES(1, 'Gaurav','Arora')
INSERT INTO dbo.checkDuplicate VALUES
(5, 'Neelima','Malhotra')
INSERT INTO dbo.checkDuplicate VALUES(4, 'Shweta','Arora')
INSERT INTO dbo.checkDuplicate VALUES(4, 'Shweta','Arora')
INSERT INTO dbo.checkDuplicate VALUES(2, 'Meghna','Arora')

Thanks,
Gaurav Arora
http://stuff4mdesktop.blogspot.com/

Is This Answer Correct ?    29 Yes 6 No

what is the difference between union and union all..

Answer / praveen k. agrawal

union is used For select distinct values from two tables
where as union all is used For select all values including
duplicates from the tables.

For Example Table X contain values(a,b,c) and Table Y
contain values(c,d,e)
In Case Of Union we will get Result-a,b,c,d,e
and other hand (Union All ) we will get-a,b,c,c,d,e.

Is This Answer Correct ?    23 Yes 0 No

what is the difference between union and union all..

Answer / sugumar.p

Suresh is perfectly correct

Is This Answer Correct ?    20 Yes 7 No

what is the difference between union and union all..

Answer / banti

Jyothsna,

you are correct too.

Regards,
Bunty

Is This Answer Correct ?    19 Yes 8 No

what is the difference between union and union all..

Answer / vishal

UNION removes duplicates, whereas UNION ALL does not.

In order to remove duplicates the result set must be
sorted, and this may have an impact on the performance of
the UNION, depending on the volume of data being sorted,
and the settings of various RDBMS parameters ( For Oracle
PGA_AGGREGATE_TARGET with WORKAREA_SIZE_POLICY=AUTO or
SORT_AREA_SIZE and SOR_AREA_RETAINED_SIZE if
WORKAREA_SIZE_POLICY=MANUAL ).

Basically, the sort is faster if it can be carried out in
memory, but the same caveat about the volume of data
applies.

Of course, if you need data returned without duplicates
then you must use UNION, depending on the source of your
data.

I would have commented on the first post to qualify the "is
much less performant" comment, but have insufficient
reputation (points) to do so.

Is This Answer Correct ?    9 Yes 1 No

what is the difference between union and union all..

Answer / anjani kumar

Union will return only distinct values from two tables.
Union all will return duplicate values from the two tables

Is This Answer Correct ?    7 Yes 1 No

what is the difference between union and union all..

Answer / ranjay kumar singh

union is used to select distinct values from two tables
where as union all is used to select all values including
duplicates from the tables

Is This Answer Correct ?    5 Yes 0 No

Post New Answer

More SQL PLSQL Interview Questions

What does plvtab enables you to do when you showthe contents of pl/sql tables?

0 Answers  


State few characteristics of pl/sql?

0 Answers  


How can I delete duplicate rows?

0 Answers  


what is cursor. write example of it. What are the attributes of cursor.

0 Answers  


What is sql partition?

0 Answers  






Is like operator in sql case sensitive?

0 Answers  


what is auto increment? : Sql dba

0 Answers  


What is varchar used for?

0 Answers  


Difference between aggregate function and analytical function?

3 Answers   Metric Stream,


What are the datatypes a available in PL/SQL ?

2 Answers  


When is the update_statistics command used?

0 Answers  


1 SELECT a.field1, b.field2, c.field3, d.field4 2 FROM atable a, atable b, ctable c, dtable d 3 ? 4 ORDER BY 1 What is the minimum number of joins that must be specified on line 3 in the sample code above to properly link the tables? Notice that the table "atable" is aliased twice: once as "a" and once as "b." 1. One join 2. Two joins 3. Three joins 4. Four joins 5. Five joins

6 Answers   Sonata,


Categories