Sharpeye500
Sharpeye500

Reputation: 9073

Combining query results of two different tables

First select query

Table 1:

ID   Value
131  ABC
120  DEF

Second select query

Table 2:
ID 
120
131

I want to write a single query which will fetch me combining two tables (the required output)

ID Value
120 DEF
131 ABC

Note: if there is no entry in Table2, return the data from Table1 else combine and return the result.

Any thoughts? Thanks.

Upvotes: 0

Views: 1076

Answers (4)

Kris Ivanov
Kris Ivanov

Reputation: 10598

something like that:

SELECT *
FROM Table1 t1
    LEFT OUTER JOIN Table2 t2 ON t1.ID = t2.ID

Upvotes: 0

Asha
Asha

Reputation: 4311

SELECT * 
FROM table1 LEFT JOIN table2
ON table1.ID = table2.ID

if it can't find the matching record in table2 it will fill table2 columns in the result set with null

Upvotes: 2

Randy
Randy

Reputation: 16677

select t1.id, value
from table1 t1, table2 t2
where t1.id = t2.id

Upvotes: 0

Yochai Timmer
Yochai Timmer

Reputation: 49251

SELECT * FROM Table1,Table2 WHERE Table1.ID = Table2.ID

Upvotes: 1

Related Questions