I want to know what is the difference between UNION and UNION ALL in SQL Server ?
Thank you.
6 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Satyapriya NayakRank #47 · 31.5K reputationPosted Jun 18, 2012, 1:54 AM EDT
+2
Hi Vipendra,
SQL UNION query is to combine the results of two queries together. In this respect, UNION is somewhat similar to JOIN in that they are both used to related information from multiple tables. One restriction of UNION is that all corresponding columns need to be of the same data type. Also, when using UNION, only distinct values are selected (similar to SELECT DISTINCT).
The syntax is as follows:
[SQL Statement 1] UNION [SQL Statement 2]
Say we have the following two tables,
Table Store_Information store_name Sales Date Los Angeles $1500 Jan-05-1999 San Diego $250 Jan-07-1999 Los Angeles $300 Jan-08-1999 Boston $700 Jan-08-1999
and we want to find out all the dates where there is a sales transaction. To do so, we use the following SQL statement:
SELECT Date FROM Store_Information UNION SELECT Date FROM Internet_Sales
Result: Date Jan-05-1999 Jan-07-1999 Jan-08-1999 Jan-10-1999 Jan-11-1999 Jan-12-1999
SQL UNION ALL command is also to combine the results of two queries together. The difference between UNION ALL and UNION is that, while UNION only selects distinct values, UNION ALL selects all values.
The syntax for UNION ALL is as follows:
[SQL Statement 1] UNION ALL [SQL Statement 2]
Let's use the same example as the previous section to illustrate the difference. Assume that we have the following two tables,
Table Store_Information store_name Sales Date Los Angeles $1500 Jan-05-1999 San Diego $250 Jan-07-1999 Los Angeles $300 Jan-08-1999 Boston $700 Jan-08-1999
and we want to find out all the dates where there is a sales transaction at a store as well as all the dates where there is a sale over the internet. To do so, we use the following SQL statement:
SELECT Date FROM Store_Information UNION ALL SELECT Date FROM Internet_Sales
Result: Date Jan-05-1999 Jan-07-1999 Jan-08-1999 Jan-08-1999 Jan-07-1999 Jan-10-1999 Jan-11-1999 Jan-12-1999
Aniket NarvankarRank #903 · 2.3K reputationPosted Nov 11, 2018, 6:55 PM EST
0
1.Union All Selects Duplicate Records,while Union Selects Distinct Records
2.Union All is faster than Union.Becauses Union orders the records while selecting them.
3.Both in case of Union and Union All the type and number of columns must same in both sides of select statement.Also if you are using order by clause you should use with last select statement both in case of union and union all.
0
Sourav MukherjeeRank #850 · 2.6K reputationPosted Dec 6, 2018, 11:18 AM EST
0
Union all will not remove the duplicate rows whereas it just pulls all rows from all tables matchng with your query specifics and merges them into a table. On the other hand, a UNION statement effectively works just like a SELECT DISTINCT on the results set.
0
Mageshwaran RRank #411 · 6.1K reputationPosted Nov 13, 2019, 12:20 AM EST
0
Hi vipendra verma,
Union:
1.Uninon is two table get common or distinct value only.
2.Union command is select related information from two tables,like join command.
Union All:
uninon All is two table get all values.
The main differnce between union and union all is that union all will not eliminate duplicate rows,but union all is eliminate duplicate rows.
Satyapriya NayakRank #47 · 31.5K reputationPosted Jun 18, 2012, 1:54 AM EDT
SQL UNION query is to combine the results of two queries together. In this respect, UNION is somewhat similar to JOIN in that they are both used to related information from multiple tables. One restriction of UNION is that all corresponding columns need to be of the same data type. Also, when using UNION, only distinct values are selected (similar to SELECT DISTINCT).
The syntax is as follows:
[SQL Statement 1]
UNION
[SQL Statement 2]
Say we have the following two tables,
Table Store_Information
store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999
Table Internet_Sales
Date Sales
Jan-07-1999 $250
Jan-10-1999 $535
Jan-11-1999 $320
Jan-12-1999 $750
and we want to find out all the dates where there is a sales transaction. To do so, we use the following SQL statement:
SELECT Date FROM Store_Information
UNION
SELECT Date FROM Internet_Sales
Result:
Date
Jan-05-1999
Jan-07-1999
Jan-08-1999
Jan-10-1999
Jan-11-1999
Jan-12-1999
SQL UNION ALL command is also to combine the results of two queries together. The difference between UNION ALL and UNION is that, while UNION only selects distinct values, UNION ALL selects all values.
The syntax for UNION ALL is as follows:
[SQL Statement 1]
UNION ALL
[SQL Statement 2]
Let's use the same example as the previous section to illustrate the difference. Assume that we have the following two tables,
Table Store_Information
store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999
Table Internet_Sales
Date Sales
Jan-07-1999 $250
Jan-10-1999 $535
Jan-11-1999 $320
Jan-12-1999 $750
and we want to find out all the dates where there is a sales transaction at a store as well as all the dates where there is a sale over the internet. To do so, we use the following SQL statement:
SELECT Date FROM Store_Information
UNION ALL
SELECT Date FROM Internet_Sales
Result:
Date
Jan-05-1999
Jan-07-1999
Jan-08-1999
Jan-08-1999
Jan-07-1999
Jan-10-1999
Jan-11-1999
Jan-12-1999
Thanks
Posted Jun 18, 2012, 1:50 AM EDT
When you use union-all statement, it will return duplicate values also.
http://blog.sqlauthority.com/2009/03/11/sql-server-difference-between-union-vs-union-all-optimal-performance-comparison/
http://blog.sqlauthority.com/2007/03/10/sql-server-union-vs-union-all-which-is-better-for-performance/
Aniket NarvankarRank #903 · 2.3K reputationPosted Nov 11, 2018, 6:55 PM EST
Sourav MukherjeeRank #850 · 2.6K reputationPosted Dec 6, 2018, 11:18 AM EST
Mageshwaran RRank #411 · 6.1K reputationPosted Nov 13, 2019, 12:20 AM EST
Govardhan PRank #1,969 · 744 reputationPosted Nov 18, 2019, 9:41 PM EST
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts