Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL "Not In" Query

Posted on 2015-01-24
5
Medium Priority
?
122 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 71

Accepted Solution

by:
Qlemo earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
Viewers will learn how the fundamental information of how to create a table.

886 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