Solved

Data from table1 where no match on Table2

Posted on 2009-05-15
8
335 Views
Last Modified: 2012-05-07
I would like to write a query which bring back all the data from table1 where there is match in table2.

The match uses a primary key called TableJoinKey

can anybody help.

Thanks
0
Comment
Question by:Mr_Shaw
  • 4
  • 3
8 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 100 total points
ID: 24393676
simply join:
select t1.*
  from table1 t1
 where exists ( select null from table2 t2 where t2.TableJoinKey = t1.tableJoinkey ) 

Open in new window

0
 

Author Comment

by:Mr_Shaw
ID: 24393714
what would happen if i used a left or right join
0
 
LVL 31

Assisted Solution

by:RiteshShah
RiteshShah earned 400 total points
ID: 24393796
you can use this one also.

select t1.* from table1 t1 left join table2 t2 on t1. TableJoinKey  =t2. TableJoinKey  where t2.fieldone=NULL
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 31

Assisted Solution

by:RiteshShah
RiteshShah earned 400 total points
ID: 24393806
above query will return only records not match with table2, if you want records with match, use this one.


select t1.* from table1 t1 left join table2 t2 on t1. TableJoinKey  =t2. TableJoinKey  where t2.fieldone is not NULL
0
 
LVL 31

Assisted Solution

by:RiteshShah
RiteshShah earned 400 total points
ID: 24393839
you can use AngelIII's solution also,

for records match:

select t1.*
  from table1 t1
 where exists ( select null from table2 t2 where t2.TableJoinKey = t1.tableJoinkey )


for records not match:

select t1.*
  from table1 t1
 where not exists ( select null from table2 t2 where t2.TableJoinKey = t1.tableJoinkey )
0
 

Author Comment

by:Mr_Shaw
ID: 24393856
ok i'll have a play arround...

Please could someone tell me what would happen if I used a left or right join.
0
 
LVL 31

Assisted Solution

by:RiteshShah
RiteshShah earned 400 total points
ID: 24393877
if you use left join than you will get all records from table 1 and records doesn't match with table1, will come as NULL in table2. have a look at following query.

select t1.* from table1 t1 left join table2 t2 on t1. TableJoinKey  =t2. TableJoinKey

reverse process than above if you use right join.
0
 

Author Closing Comment

by:Mr_Shaw
ID: 31581849
Thanks
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question