表結構是這樣的:
stu(sno,sname,sdept,sage)
sc(sno,cno,grade)
cou(cno,cname)
SELECT sdept ,
AvgGrade
FROM ( SELECT sdept ,
AVG(grade) AS AvgGrade ,
DENSE_RANK() OVER ( PARTITION BY sdept ORDER BY AVG(grade) ) AS RN
FROM ( SELECT sdept ,
sno ,
SUM(grade) AS grade
FROM stu AS a
INNER JOIN sc AS b ON b.sno = a.sno
GROUP BY sdept ,
sno
) AS a
GROUP BY sdept
) AS T
WHERE RN = 1;