Solved

Data from table1 where no match on Table2

Posted on 2009-05-15
8
336 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 143

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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.

856 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