---
title: "Retreiveing records on the basis of two different tables."  
description: "Retreiveing records on the basis of two different tables."  
author: "Chris Anderson"  
published: 2012-08-03  
updated: 2013-04-18  
canonical: https://www.mindstick.com/forum/434/retreiveing-records-on-the-basis-of-two-different-tables  
category: "mssql server"  
tags: ["mssql server"]  
reading_time: 2 minutes  

---

# Retreiveing records on the basis of two different tables.

Suppose I have [two tables](https://www.mindstick.com/forum/159646/how-to-join-two-tables-in-oracle-to-get-single-line-results) (Emp and EmpCopy) in [sql server](https://www.mindstick.com/articles/12999/what-is-table-valued-function-in-sql-server) [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) with following [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science):

**Case 1:** Data in **Emp** and **EmpCopy** [table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap) [present](https://answers.mindstick.com/qa/96635/explain-about-the-various-features-present-in-ms-access) like below:

**Table:** Emp

**Id** **Name**

----------------------

1 Emp_Name

2 Emp_Age

3 Emp_City

**Table:** EmpCopy

**Id** **Name**

--------------------

1 Emp_Name

2 Emp_Age

**Case 2:** Data in **Emp** and **EmpCopy** table present like below:

**Table:** Emp

**Id** **Name**

--------------------

1 Emp_Name

2 Emp_Age

**Table:** EmpCopy

**Id** **Name**

---------------------

1 Emp_Name

2 Emp_Age

3 Emp_City

Now according to cases given above I want [retrieve](https://www.mindstick.com/forum/34400/how-to-retrieve-form-values-in-controller-action) [records](https://www.mindstick.com/forum/34640/how-to-create-a-stored-procedure-for-display-all-records) from both the tables, just like given below

## Case 1:

**Id** **Emp_Column** **EmpCopy_Column**

1 Emp_Name Emp_Name

2 Emp_Age Emp_Age

3 Emp_City NULL

## Case 2:

**Id** **Emp_Column** **EmpCopy_Column**

1 Emp_Name Emp_Name

2 Emp_Age Emp_Age

3 NULL Emp_City

Please reply ASAP.

Thanks in advance.

## Replies

### Reply by Chris Anderson

thanks shankar.

### Reply by Shankar M

Hi Rohit,\
Considering your First Case, Here is the Query\
SELECT A.ID,A.NAME,B.NAME FROM EMP A LEFT OUTER JOIN EMPCOPY B ON A.ID = B.ID AND A.NAME =B.NAME\
And the Second case which is a little reverse of First One,\
So I have used the Same [Tables](https://www.mindstick.com/articles/336597/introduction-of-html-tables-for-web-development) but changing the Type of Join \
SELECT B.ID,A.NAME,B.NAME FROM EMPCOPY A RIGHT OUTER JOIN EMP B ON A.ID = B.ID AND A.NAME =B.NAME\
Ideally, If you consider only the ID Column in both tables then, the query turns out to be\
First Case,SELECT A.ID,A.NAME,B.NAME FROM EMP A LEFT OUTER JOIN EMPCOPY B ON A.ID = B.ID\
Second Case,\
SELECT B.ID,A.NAME,B.NAME FROM EMPCOPY A RIGHT OUTER JOIN EMP B ON A.ID = B.ID \
Thanks,Shankar\
\


---

Original Source: https://www.mindstick.com/forum/434/retreiveing-records-on-the-basis-of-two-different-tables

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
