Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Fetch data from two tables

Author  Topic 

amarflora
Starting Member

4 Posts

Posted - 2008-12-17 : 12:04:19
Hello there,

I have two tables with relationship with 'id' column in both tables. I a select command to fetch all the data from the 1st table but leave the ones which are already present in the second table.

For example

In the first table, there are three rows with ID being 1,2 and 3.
In the second table, there is one row with ID 1

I want to output to be 2 and 3(Leaving 1 as it exists in second table)

Thanks a lot for your help!!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-17 : 12:06:07
[code]SELECT t1.*
FROM table1 t1
LEFT JOIN table2 t2
on t1.id=t2.id
where t2.id is null[/code]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-17 : 12:06:57
[code]SELECT t1.*
FROM table1 t1
WHERE NOT EXISTS (SELECT 1 FROM table2 WHERE id=t1.id)[/code]
Go to Top of Page

amarflora
Starting Member

4 Posts

Posted - 2008-12-17 : 14:08:20
THANKS A LOT MATE. WORED FINE!!!:)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-17 : 23:20:33
welcome
Go to Top of Page
   

- Advertisement -