Return All Records From Two Table In SQL Server 2008

In this article I will tell you how to get all the records from two table in SQL Server.
  • 1922

In this article I will tell you how to get all the records from two table in SQL Server.

This question is generally asked in interviews.

In SQL Server there is a way to combine multiple datasets into one dataset with the help of UNION ALL operator. UNION ALL operator does not remove duplicate rows and return all the records from both tables.

Lets take a simple example of how to work with UNION ALL operator. Suppose we have two tables Employee and EmployeeeDetails with some records. We are interested to find out the record which are present in both table [Employee] and table [EmployeeeDetails].

Select the record from table Employee.

UnionAllTable01.jpg

Select the record from table EmployeeeDetails.

UnionAllTable02.jpg

Using UNION ALL operator to retrieve the records which are present in Employee and EmployeeeDetails table.

UnionAllTable03.jpg

Note- Each columns in a table must have the same data types. The UNION ALL select all the records from both table must be in the same order.

© 2020 DotNetHeaven. All rights reserved.