Tables:
tblStudents:
ID, Student Name
1, John
2, Mark
3, Fred
tblEnrolledSubjects:
ID, Student ID, Subject ID, Score
1, 1, 1, 75
2, 1, 2, 75
3, 1, 3, 75
4, 1, 4, NULL
5, 2, 1, 75
6, 3, 1, 75
7, 3, 2, 80
8, 3, 3, 85
tblSubject:
ID, Subject Name
1, Maths
2, English
3, Science
4, History
I want to return all distinct students that have a score of over (say 70) for all subjects. SELECT [Student Name]
Students can be enrolled in 1 or more subjects. If a student has even one subject not of required score then they should not be listed.
From he above data I would expect to see
Mark
Fred
What would the SQL query be for this?
If a NULL
value doesn't count against you, then
SELECT tblStudents.[Student Name]
FROM tblEnrolledSubjects RIGHT JOIN tblStudents
ON tblEnrolledSubjects.[Subject ID] = tblStudents.ID
GROUP BY tblStudents.[Student Name]
HAVING (Min(tblEnrolledSubjects.[Score])>=70);
SELECT t.Studentname
FROM tblStudents t JOIN
(select count(*) cnt, min(nz(score,0)) minscore, StudentID
FROM tblEnrolledSubjects
GROUP BY StudentID) s
ON t.Studentid = s.studentid
WHERE minscore>=70 --if mark is greater than or equal to 70
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.