Solved

SQL "Not In" Query

Posted on 2015-01-24
5
106 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:SimonAdept
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:SimonAdept
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Restore Procedure question 4 30
MS SQL Backup 24 70
C# SQL BULK INSERT CLASS 5 35
SQL Script to find duplicates 16 19
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

705 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

17 Experts available now in Live!

Get 1:1 Help Now