I have this code in Access:
SELECT DISTINCT p.course, p.date
FROM Student s, Participation p
WHERE s.id = d.student AND footballer = NO
When I run it, I get 10 rows. So I use it as a subquery the next time:
SELECT c.course, c.date
FROM Course c
WHERE NOT EXISTS
(SELECT DISTINCT p.course, p.date
FROM Student s, Participation p
WHERE s.id = d.student AND footballer = NO)
and I get no results at all. So I remove the NOT in NOT EXISTS and I get all 15 rows that are in the Course table. So it seems to me that course and date from the subquery read from Course from the main query instead of from Participation from the subquery. But how is this possible since I have created different alias names and even the source tables are different (though they both contain columns with names course and date) and how do I solve the problem?
Your sub-query is not related to the main query. Try something like this?
This now takes every record in
Courseand runs the NOT EXISTS check for each row, filtering theParticipationtable by thecourseanddatefields.Your example just ran the sub query once. It returned 10 records, and so the
NOT EXISTSalways failed.