How to list only records that DO NOT match the criteria?

R

Richard Hollenbeck

If I list the students in the students table I get 204 students. But if I
add the studentsInCourses table to the query I get 129 students. This is
because when a student is dropped from a course they are not automatically
dropped from the students table.

I tried the following but it returned zero records:

SELECT students.studentID
FROM students INNER JOIN studentsInCourses ON students.studentID =
studentsInCourses.studentID
WHERE (((students.studentID)<>[studentsInCourses].[studentID]));
 
R

Richard Hollenbeck

Thanks, but never mind. I figured it out. Here's what I did:

SELECT *
FROM students
WHERE studentID NOT IN (Select studentID From studentsInCourses);

Now it returns exactly 75 students, the difference between 204 and 129.
 
K

Ken Snell [MVP]

That type of join can run fairly slow. Try this:

SELECT students.*
FROM students LEFT JOIN studentsInCourses
ON students.studentID = studentsInCourses.studentID
WHERE studentsInCourses.studentID Is Null;

--

Ken Snell
<MS ACCESS MVP>

Richard Hollenbeck said:
Thanks, but never mind. I figured it out. Here's what I did:

SELECT *
FROM students
WHERE studentID NOT IN (Select studentID From studentsInCourses);

Now it returns exactly 75 students, the difference between 204 and 129.

Richard Hollenbeck said:
If I list the students in the students table I get 204 students. But if I
add the studentsInCourses table to the query I get 129 students. This is
because when a student is dropped from a course they are not automatically
dropped from the students table.

I tried the following but it returned zero records:

SELECT students.studentID
FROM students INNER JOIN studentsInCourses ON students.studentID =
studentsInCourses.studentID
WHERE (((students.studentID)<>[studentsInCourses].[studentID]));
 
R

Richard Hollenbeck

WOW! Thanks. The query currently runs rather quickly, but perhaps it will
slow down as the database grows. I'll examine (and then try) this and get
back to the group. Thanks again.

Rich


Ken Snell said:
That type of join can run fairly slow. Try this:

SELECT students.*
FROM students LEFT JOIN studentsInCourses
ON students.studentID = studentsInCourses.studentID
WHERE studentsInCourses.studentID Is Null;

--

Ken Snell
<MS ACCESS MVP>

Richard Hollenbeck said:
Thanks, but never mind. I figured it out. Here's what I did:

SELECT *
FROM students
WHERE studentID NOT IN (Select studentID From studentsInCourses);

Now it returns exactly 75 students, the difference between 204 and 129.
if
I
add the studentsInCourses table to the query I get 129 students. This is
because when a student is dropped from a course they are not automatically
dropped from the students table.

I tried the following but it returned zero records:

SELECT students.studentID
FROM students INNER JOIN studentsInCourses ON students.studentID =
studentsInCourses.studentID
WHERE (((students.studentID)<>[studentsInCourses].[studentID]));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top