SQL Query to convert single row from multiple tables into single row in a table

I have a scenario like Have 7 tables. All these 7 tables have same metadata structure and also all have 2 columns. In that, One column is same in all 7 tables (Joining column, But not a primary column) and another column has different values.
Below have a example for 2 tables. How can we achieve for 7 tables?
Tab1
====
loc_name mem_name
HYD RAJU
HYD MOHAN

Tab2
====
loc_name mem_name
HYD RAVI
HYD MOHAN

OUTPUT:
========
loc_name mem_exist1 mem_exist2
HYD RAJU NULL
HYD NULL RAVI
HYD MOHAN MOHAN

Showing Answers 1 - 12 of 12 Answers

Shrushti

  • Jul 20th, 2016
 

Code
  1.  

  2. SELECT loc-name,mem-name

  3. FROM tab1,tab2,tab3

  4. WHERE tab1.loc-name=tab2.loc-name

  5. AND

  6. tab2.loc-name=tab3.loc-name;

  Was this answer useful?  Yes

Laxmi

  • Aug 5th, 2016
 

Code
  1. SELECT LOC-NAME,MEM-NAME1,MEM-NAME2

  2. FROM TAB1,TAB2

  3. WHERE TAB1.LOC-NAME = TAB2.LOC-NAME(+);

  4.  

  Was this answer useful?  Yes

Pavan

  • Oct 1st, 2016
 

Hi use sub-query with left join

  Was this answer useful?  Yes

Tmas

  • Nov 3rd, 2016
 

Use outer join

Code
  1. SELECT ISNULL(Tab1.loc_name, Tab2.loc_name), Tab1.mem_name, Tab2.mem_name FROM Tab1 FULL OUTER JOIN Tab2 ON Tab1.mem_name = Tab2.mem_nam

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions