Sql Table Join?

  • Thread starter Thread starter Soniya
  • Start date Start date
S

Soniya

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.

any code example will be much helpful

Thanks

Soniya
 
Soniya said:
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.

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top