Solved

SQL "Not In" Query

Posted on 2015-01-24
5
113 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 69

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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 combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

777 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