Return Common Records From Two Table In SQL Server 2008

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

In this article I will tell you how to get all the common 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  operator. UNION operator remove any duplicates that exist.

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

Select the record from table User.

UnionTable01.jpg

Select the record from table Users.


UnionTable02.jpg

Using UNION operator to retrieve the common records which are present in User and Users table.


UnionTable03.jpg

Note

  • Each columns in a table must have the same data types.
  • Each query must have the same number of columns.
  • Column name in result set are taken from first query. 
© 2020 DotNetHeaven. All rights reserved.