Introduction:


In this article we will explain how to use union command in SQL Server or what is union command In SQL Server or how to combine multiple tables’ record in a single result set or how to select distinct records from multiple tables with example.


Description:


The UNION command is used to combine the information from two or more tables, when using the UNION command all selected columns need to be of the same data type. With UNION  command only distinct values are selected.

For Example:

I have two tables Student and Employee both table columns and data type are same.

Student Table

select Name,Mobile,address1 from Student

Result:

Union in SQL Server

Employee Table

select Name,Mobile,address1 from employee

Result:

Union in SQL Server

Show above student and employee both tables have same record Rajeev which is highlighted.

Union

select Name,Mobile,address1 from Student 
union
select Name,Mobile,address1 from employee

Result:

Union in SQL Server

Shows above example using with union command combine both table records with removing duplicate data. Rajeev has a record in both tables but when we use union command Rajeev record show only once.

Thanks.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like