Solved

SQL "Not In" Query

Posted on 2015-01-24
5
110 Views
Last Modified: 2015-01-24
I have three tables.
1. Jobs: jobid, jobname, payrate
2. JobAssign: jobid, empID
3. Employees: EmpID, EmpName

The Jobs table contains the details of the jobs.  The employee table contains the details of the employees.
The JobAssign tables puts the two together. In some cases and employee can have two jobs.  So if I searched JobAssign for a specific ID, I should get all the JobIDs for which that employee is assigned:

select * from Jobs inner join JobAssign on Jobs.Jobid = JobAssign.jobid where JobAssign.EmpId = 1

I'm trying to set it up so I can find all the jobs that an employee has NOT been assigned too.

I can not figure out how to do write the query.

Thanks for your help!!!
0
Comment
Question by:slightlyoff
  • 2
  • 2
5 Comments
 
LVL 18

Expert Comment

by:Simon
ID: 40568481
select * from jobs inner join jobassign on jobs.jobid=jobassign.jobid
where jobassign.jobid not in (select jobID from jobassign where empid =1)
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40568492
select * from jobs where jobid not in (select jobid from jobassign where empID = 1);
select * from jobs where not exists (select * from jobassign where jobs.jobid = jobid and empID = 1);

Open in new window

are two of the common approaches.
0
 
LVL 1

Author Comment

by:slightlyoff
ID: 40568493
Thanks for the quick reply.  I tried that and I tried:

select * from jobs inner join jobassign on jobs.jobid=jobassign.jobid where jobs.jobid not in (select jobID from jobassign where empid = 1)

There are currently two jobs in the Jobs table.  One job has been assigned to the Employee in the JobAssign table, so this query should pull the other job.  But both querys return nothing.
0
 
LVL 1

Author Closing Comment

by:slightlyoff
ID: 40568495
Thank you. That worked perfectly!
0
 
LVL 18

Expert Comment

by:Simon
ID: 40568517
Doh! The reason you were getting no rows returned is that the job you were expecting to see wasn't assigned to ANY employee.

I should have changed your join to a left join:
select * from jobs LEFT join jobassign on jobs.jobid=jobassign.jobid
where jobs.jobid not in (select jobID from jobassign where empid =1) 

Open in new window


This version of the query may be useful to you if you want to see which jobs have OTHER employees assigned to them and which have NO employees assigned to them.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

863 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

24 Experts available now in Live!

Get 1:1 Help Now