Hi
Explain the SQL UNION statement. Write SQL syntax for the SQL UNION statement along with an example. What is the difference between UNION and UNION ALL ?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
SenthilkumarPosted May 17, 2012, 1:19 AM
It will not filter the duplicate records. It combines the rows rather than columns.
For example, have two tables.
SELECT id,name,address FROM Employee1
UNION
SELECT id,name,address FROM Employee2
It will combine the two table results.
Satyapriya NayakPosted May 16, 2012, 10:30 PM
UNION
The purpose of the 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
Table Internet_Sales
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:
UNION ALL
The purpose of the 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
Table Internet_Sales
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:
UNION is used to select information from two tables.But it selects only distinct records from both the table. , while UNION ALL selects all records from both the tables.
Please refer the below links
http://www.1keydata.com/sql/sqlunion.html
http://www.1keydata.com/sql/sqlunionall.html
Thanks
Posted May 16, 2012, 10:21 PM
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. With UNION, only distinct values are selected.
UNION ALL
The UNION ALL command is equal to the UNION command, except that UNION ALL selects all values.
The difference between Union and Union all is that Union all will not eliminate duplicate rows, instead it just pulls all rows from all tables fitting your query specifics and combines them into a table.
http://blog.sqlauthority.com/2008/10/15/sql-server-introduction-and-example-of-union-and-union-all/