Solved

SQL "Not In" Query

Posted on 2015-01-24
5
120 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 70

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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

627 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