架构:
Student(studentid,name,age)
Course(coursename,dept)
enroll(studentid,coursename,grade)
我需要编写 sql 来查找每个年龄组的学生姓名,其中历史和政治学系的课程成绩最高。
到目前为止,我的尝试是
SELECT
name
FROM
student
GROUP BY age
HAVING sid IN
(
SELECT
max(grade)
FROM
enroll e,enroll e1
WHERE
e.studentid = e1.studentid
AND e.coursename = (
SELECT coursename FROM course
WHERE
dname like '%History%'
)
AND e1.coursename = (
SELECT coursename FROM course
WHERE
dname like '%PoliticalScience%'
)
)