数据库:一条sql语句查询每门课程都大于80分的学生姓名

描述:已知student表有姓名-name,分数-grade,课程-course,它们多对多对多,一条sql语句查询每门课程都大于80分的学生姓名?
分析
①每门课程,涉及到课程数量统计

SELECT COUNT(DISTINCT(s.course),s.course) courseCount FROM student s

②课程大于80分的学生

select s1.name,count(grade) from student s1 where s1.grade>80 group by s1.name

③每门课程都大于80分的学生

SELECT
	s2.NAME 
FROM
	( SELECT s1.NAME, count( grade ) moreThan80Count FROM student s1 WHERE s1.grade > 80 GROUP BY s1.NAME ) s2 
WHERE
	s2.moreThan80Count =(
	SELECT
		COUNT( DISTINCT ( s.course ), s.course ) courseCount 
FROM
	student s)

你可能感兴趣的:(数据库,习题,sql)