Solved

Select where table1 not in table2

Posted on 2006-07-20
3
748 Views
Last Modified: 2012-05-05
I have 2 tables.

Table1 is made of 2 primary keys, table_id and job_id along with a bunch of other values (lets just use job_name for now).
Table2 is made of 2 primary keys, table_id and job_id along with a bunch of other values (Don't care what for this example).

I need to select the job_name from table1 where that entries table_id and job_id are not in table2.

How can I do this?  I was using an outter join when I wanted table 2 with table 1 but now that I only want table1 when there is no matchig entry in table 2!!

Thanks!
0
Comment
Question by:JRamos1200
  • 2
3 Comments
 
LVL 25

Accepted Solution

by:
Mr_Peerapol earned 125 total points
Comment Utility
SELECT t1.*
FROM Table1 t1
WHERE NOT EXISTS (SELECT 1 FROM Table2 t2 WHERE t1.table_id = t2.table_id AND t1.job_id = t2._jobid)
0
 

Author Comment

by:JRamos1200
Comment Utility
That did it!  Thanks.  What does select 1 mean anyway?
0
 
LVL 25

Expert Comment

by:Mr_Peerapol
Comment Utility
Just the constant number 1. In the EXISTS clause the list of fields returned by inner SELECT doesn't matter because EXISTS() just checks whether there is a record returned by the SELECT statement no matter what columns included.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Help in Understanding a SQL Query 7 24
SQL server 2008 SP4 29 31
SQL JOIN 6 31
sql calculate averages 18 24
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now