20 May 2016 Sudhanshu Ranjan 699

What is the difference between UNION and UNION ALL?

UNION and UNION ALL both are merged the data of two different tables in single table. UNION display only the unique data in both table whereas UNION ALL display duplicate data also. Column name should be match on both table.

UNION ALL is typically faster than UNION.

Example :

Employee Table

CREATE TABLE [dbo].[employee](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[name] [varchar](50) NULL,
 CONSTRAINT [PK_employee] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

Salary Table

CREATE TABLE [dbo].[salary](
	[id] [int] NULL,
	[salary] [int] NULL
) ON [PRIMARY]

 

 

UNION

select ID from employee UNION 
select id from salary

Output

 

UNION ALL

select ID from employee UNION ALL
select ID from salary

Output

 

 

 

 

 

 

You can share at :

About Author
Sudhanshu Kumar Ranjan | Lead Engineer at HCL Technologies | Expertise in Microsoft Technology | Author | Blogger | Programmer
Know More
Connect with him on Facebook | Google+ | LinkedIn | Twitter

Recommended Post

Leave a comment

About Dotnet Cube

It's my blog - Dotnet Cube, where you will learn and get solutions about programming, web designing and development. I started this website to share my knowledge and experience which might help someone.

You can get more details about me and my blog at About Us page.

Subscribe Newsletter

Subscribe to our monthly newsletter to get the latest industry news, Media Group updates and our current specials.

Follow On Us...

Home | About | Advertise | Copyrights | Privacy | Contact | Admin

Copyrights © 2015-2017 dotnetcube.com. All rights reserved