C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » Databases »

Difference between UNION and UNION ALL in SQL


Posted Date: 28 Jun 2009    Resource Type: Articles    Category: Databases
Author: Viji RAJKUMARMember Level: Diamond    
Rating: 1 out of 5Points: 12



In this article we will see the difference between UNION and UNION ALL Sql Keywords.

Syntax:

[SQL Statement 1]


UNION [ALL]


[SQL Statement 2]

Where ALL statement is optional.

Both the Sql statements must consist of equal number of fields.


Similarties:


The UNION and UNION ALL Sql Keywords are used to combine two or more sql queries and return the result set consisting of a single set of all queries mentioned in there.


Differences:

The main difference between UNION and UNION ALL is that UNION retrieves distinct values (With out duplicates) where as UNION ALL retrieves all values from the result set including duplicates.


Example

Consider Employees Table of NorthWind database.


UNION ALL

The following query is used to display the EmployeeId and lastName.

SELECT EmployeeID, lastName
FROM [dbo].[Employees]

UNION ALL

SELECT EmployeeID, lastName
FROM [dbo].Employees


RESULT


EmployeeId LastName

5 Buchanan
8 Callahan
1 Davolio
9 Dodsworth
5 Buchanan
8 Callahan
1 Davolio
9 Dodsworth

The repetition of same records twice. Thus duplicate records exists in this result set.

UNION

The following query is used to display the EmployeeId and lastName.

SELECT EmployeeID, lastName
FROM [dbo].[Employees]

UNION

SELECT EmployeeID, lastName
FROM [dbo].Employees


RESULT

EmployeeId LastName

5 Buchanan
8 Callahan
1 Davolio
9 Dodsworth


Here only distinct values are returned. Thus no duplicate records exists in this result set.



Responses

Author: Manigandan    29 Jun 2009Member Level: Gold   Points : 2
Hi,

Nice one keep posting more...

We can also use Union in a join Query like this,

SELECT employees.Lastname, employees.Firstname, invoices.Sale, invoices.Price
FROM employees
INNER JOIN invoices
ON employees.id = invoices.EmployeeID
UNION
SELECT employees2.Lastname, employees2.Firstname, invoices.Sale, invoices.Price
FROM employees2
INNER JOIN invoices
ON employees2.id = invoices.EmployeeID;



Thanks,
Mani



Author: Viji RAJKUMAR    29 Jun 2009Member Level: Diamond   Points : 2
hi Manig,

Thanks For your response.

In this article I took a very simple example to explain the difference between UNION and UNION ALL.

Event if we want to include specific where clause or ORDER by clause we can add it.



SELECT EmployeeID, lastName
FROM [dbo].[Employees] ORDER BY EmpID

UNION

SELECT EmployeeID, Address
FROM [dbo].[Employees1] WHERE EMPID NOT IN (1001,2001)




Note:

UNION ALL works faster than UNION as it does not perform select distinct.

If you know that you should combin all recordset use UNION ALL





Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Difference between UNION and UNION ALL in SQL  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: ADO.NET and Database Interview questions
Previous Resource: Locks in SQL Server
Return to Discussion Resource Index
Post New Resource
Category: Databases


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use