Write an sql query to select the names of the students who have attended all the lectures of professor 'JOHN' but not any lecture of professor 'JOSEPH'.
I have written this query, but it is giving me the name of student who are taking the lecture from joseph also.
select distinct s.studentname
from Student s,Attendance a,Lecture l
where s.StudentId=a.studid
and l.lecId = a.lectureid
and l.professor='JOHN'
and l.professor != 'JOSEPH';
I am making some error in above query but not able to identify the same.
here is the table structure
Student table
# StudentId, StudentName, Sex
'1', 'AMY', 'M'
'2', 'JACK', 'M'
'3', 'TONY', 'M'
'4', 'TARA', 'M'
'5', 'SARAH', 'F'
'6', 'TOM', 'F'
Lecture Table
# LecId, Subject, Professor
1, MATH, JOHN
2, MATH, JOSEPH
3, PHY, MARK
4, PHY, MAX
5, PHY, JOHN
6, CHEM, JOHN
7, CHEM, JOSEPH
8, HISTORY, JOSEPH
Attendance table
# StudId, LectureId
'1', '1'
'1', '2'
'3', '1'
'2', '5'
'2', '6'
'3', '4'
'1', '6'
'4', '5'
'5', '1'
'5', '2'
'1', '3'
'1', '4'
'1', '5'
'1', '6'