Solved

Select where table1 not in table2

Posted on 2006-07-20
3
752 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
ID: 17147254
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
ID: 17147495
That did it!  Thanks.  What does select 1 mean anyway?
0
 
LVL 25

Expert Comment

by:Mr_Peerapol
ID: 17147523
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET 5 Templates 2 66
What is format f12.8 for a CSV file 6 41
Help with Sorting Full Text results 2 14
ms sql last 8 weeks as columns 5 28
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

920 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

12 Experts available now in Live!

Get 1:1 Help Now