Sponsored By
MindStick SurveyManager
Advertise with Us
Advertisement
Advertise with Us
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog
Report Abuse Form
Reason:    
   

Home >> Database >> MSSQL Server 2000/2005/2008/2012 >> Retreiveing records on the basis of two different tables.
Author Post

Rohit Kesharwani


Total Post: 54
Member Since: 8/8/2011
Points: 385
Retreiveing records on the basis of two different tables.
Posted On: 8/3/2012 8:01:06 AM

Suppose I have two tables (Emp and EmpCopy) in sql server database with following data:

Case 1:  Data in Emp and EmpCopy  table present 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 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.

Author Post

Shankar M


Total Post: 25
Member Since: 2/9/2013
Points: 169
Re: Retreiveing records on the basis of two different tables.
Posted On: 4/9/2013 9:53:14 PM
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 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


Author Post

Rohit Kesharwani


Total Post: 54
Member Since: 8/8/2011
Points: 385
Re: Retreiveing records on the basis of two different tables.
Posted On: 4/18/2013 4:49:56 AM
thanks shankar.
Report Abuse Form
Reason:    
 
Total Online Users: 2878
Advertisement
MindStick DataConverter
Advertise with Us
  
Copyright © 2013MindStick. All Rights Reserved.