Union in SQL Server

Union: Union will combine the result of two sql queries into a single result set.

Union Specifies that multiple result sets are to be combined and returned as a single result set

Syntax:

Points to remember about union operator.

  • The number and the order of the columns must be the same in all queries.
  • The data types must be compatible.

Use of Order by Clause with Union: It’s important to know the order by clause.  using order by clause before union is incorrect, you may use order by clause after final union clause. see the below example

 

UNION and UNION ALL :

Union will not show duplicate row while “union all” display all row including duplicate rows in result set.

 

Union in SQL Server
Union in SQL Server