I have the next table: code, year, day
I want to find the code of the courses that was taken in 2000 in Sunday and Monday.
So I wrote the next query:
Select DISTINCT dbo.Course.Code
From dbo.Course
Where dbo.Course.CourseYear = 2000 AND
EXISTS (Select * From dbo.course Where (dbo.course.day = 'Sunday' and dbo.Course.CourseYear = 2000))and
EXISTS (Select * From dbo.course Where (dbo.course.day = 'Monday' and dbo.Course.CourseYear = 2000))
It’s return me the courses that were given in 2000, in Sunday or Monday. Why?
You need to enforce that the same course record is being checked in both exists statement.
Your exists statement only ensure that there exists any course that was offered in sunday and any course that was offered on monday
Revising your query:
A better written version
Also note that your schema is not normalized. You should have two tables: