Sometimes I get a syntax error, and sometimes I just get the wrong results. Therefore, I have a table called "Enrollment" and a table "Prerequisite", they look like this:
Enrollment(StudentNo, CourseNo, Term, Section, Mark) StudentNo and CourseNo are primary keys Prerequistie(CourseNo, PrereqCourseNo) CourseNo and PrereqCourseNo are primary keys
So, now I want to find students who entered a course that he did not have a precondition, that is, the course has a precondition in the table of preconditions, and the student did not have the necessary condition. The student did not have a prerequisite for if there is a precondition in which he / she did not register, or he / she did not fulfill the precondition. A student has failed the course if the s / he course mark is less than 50.
So here is what I got:
SELECT distinct Enrollment.StudentNo, enrollment.CourseNo, prereqNeeded.prereq from Enrollment, (SELECT ENROLLMENT.CourseNo, PrereqCourseNo, StudentNo from enrollment, prerequisite where ENROLLMENT.CourseNo = PREREQUISITE.CourseNo AND NOT EXISTS (SELECT StudentNo, MARK FROM ENROLLMENT WHERE enrollment.CourseNo = PrereqCourseNo AND MARK >= 50)) prereqNeeded WHERE Enrollment.StudentNo = prereqNeeded.StudentNo and prereqNeeded.CourseNo = ENROLLMENT.CourseNo
source share