Home > DeveloperSection > Forums > SQL UNION Operator
Ankit Singh

Total Post:341

Points:2389
Posted on    November-03-2015 10:00 PM

 .NET SQL Server 
Ratings:


 1 Reply(s)
 213  View(s)
Rate this:
what is use of union and union all Operator in sqlserver.


aditya kumar Patel

Total Post:244

Points:1736
Supported
Posted on    November-03-2015 10:12 PM

The UNION operator is used to combine the result-set of two or more SELECT statements.

Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order.

SQL UNION Syntax

SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;

Note: The UNION operator selects only distinct values by default.

SQL UNION ALL Syntax

SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;

Note : The UNION operator selects All (duplicate values also) values by default.

For Exp:

Union

SELECT City FROM Customer

UNION

SELECT City FROM Supplier

ORDER BY City

 

Result :

 

 

Union All

 

SELECT City FROM Customer

UNION

SELECT City FROM Supplier

ORDER BY City

 

Result:

 

 

 

 


Don't want to miss updates? Please click the below button!

Follow MindStick