SELECT 课程,
SUM(奖金) AS 奖金
FROM ( SELECT 课程,
学号,
成绩
FROM 考试 AS a
WHERE ( SELECT COUNT(*)
FROM 考试
WHERE 课程 = a.课程
AND 学号 <> a.学号
AND 成绩 > a.成绩
) < 3
) AS b
GROUP BY 课程