Hello I'm trying to run a particular query for my final year project where I want to find out the number of undergraduate students studying computer science in 2014 & 2016. The tables I'm using are coursesEnrollments & courses to record the number of students enrolled on the Computer Science course.
I got help from my supervisor and he suggested me of using self-join on courseEnrollments just so that it's easier find the same CS enrollments for students enrolled in 2014 & 2016. The query below shows you how I wrote it:
SELECT COUNT(*)
FROM courseEnrollment
WHERE StudentID IN
(SELECT X.StudentID
FROM courseEnrollment Y
JOIN courseEnrollment X
ON Y.courseID = X.courseID
JOIN courses C
ON Y.courseID = C.courseID
WHERE C.courseName = 'Computer Science'
AND X.StartAcademicYear = 2014
AND Y.StartAcademicYear = 2016);
When I ran it on MySQL, I get the wrong result (which is 4 not 6) because it only counts the students enrolled on Computer science in 2014 & not 2016. Please tell me how I can fix this problem. Thank you!
P.S. I only added "X.StudentID" in the inner query to stop MySQL from getting confused with the StudentID attribute.Changing it to "Y.StudentID" gives me 2. Again, I want to know why it's not counting all those 6 students (2 enrolled in 2016 & 4 enrolled in 2014) that are enrolled on Computer Science.