Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple SQL syntax - NOT IN

Status
Not open for further replies.

smooveb

IS-IT--Management
Jul 20, 2001
161
US
Hi everyone,

I've got two tables, that should be linked by common field course_code. The tables are called Courses and Emp_Qualifications.

How can I write a simple statement that shows me all Courses that have course_codes not found in the Emp_Qualifications table?

Thanks!
B
 
SELECT C.*
FROM Courses C LEFT JOIN Emp_Qualifications E ON C.course_code = E.course_code
WHERE E.course_code IS NULL

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
YOu want to use a left join, with a where (join column) IS NULL condition applied to the RIGHT table. The left join will match on course_code = course_code, and where there is not a match report NULL. So the where condition will limit your result to only those without a match. Does this make sense?

Anyway, here is the query to do it:
Code:
select a.*
from Courses a
left join Emp_Qualifications b
on a.course_code = b.course_code
where b.course_code is null

Post back with any question, my explanation may not be so good.

Hope this helps,

Alex

Ignorance of certain subjects is a great part of wisdom
 
And as you wanted the NOT IN syntax:
SELECT * FROM Courses
WHERE course_code NOT IN (SELECT course_code FROM Emp_Qualifications)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top