Thread: Sql Table Join?
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jamie Collins Jamie Collins is offline
external usenet poster
 
Posts: 593
Default Sql Table Join?

"Soniya" wrote ...

I have two table from which i am importing Data using SQl
Query. Both table got several identical filelds while
some fields do not appear in both (some fields are in
table A but not in table B and vice versa)

Is it possible that I can get data from both tables to a
single sheet either with identical fields only or even
including un identical fields?

Identical fields only will also help me.


Rows common to both tables:

SELECT T1.MyKeyCol, T1.MyDataCol
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.MyKeyCol = T2.MyKeyCol;

Rows in table1 that are not in table2:

SELECT T1.MyKeyCol, T1.MyDataCol
FROM Table1 T1
LEFT JOIN Table2 T2
ON T1.MyKeyCol = T2.MyKeyCol
WHERE T2.MyKeyCol IS NULL;

Rows in table2 that are not in table1:

SELECT T2.MyKeyCol, T2.MyDataCol
FROM Table1 T1
RIGHT JOIN Table2 T2
ON T1.MyKeyCol = T2.MyKeyCol
WHERE T1.MyKeyCol IS NULL;

Rows from both tables, no duplicates:

SELECT T1.MyKeyCol, T1.MyDataCol
FROM Table1 T1
UNION
SELECT T2.MyKeyCol, T2.MyDataCol
FROM Table2 T2;

Rows from both tables, including duplicates:

SELECT T1.MyKeyCol, T1.MyDataCol
FROM Table1 T1
UNION ALL
SELECT T2.MyKeyCol, T2.MyDataCol
FROM Table2 T2
ORDER BY 1;

Jamie.

--