# 课程表, 学生表, 学生成绩表 (建表数据sql语句)
1: 查询 id=1 的课程 比 id =2 的课程 成绩高的学生和 学号
2: 查询平均成绩大于60分的同学学号和平均成绩
3: 查询所有同学的学号,姓名,选课数, 总成绩
4: 查询 没有学全 所有课程的同学的 学号, 姓名.
5: 查询 选了 数学和英语的 同学的平均分
# 第一题
SELECT
ta.student_no
FROM
score ta ,
score tb
WHERE
ta.student_no = tb.student_no
and ta.course_id = 1
and tb.course_id = 2
and ta.score > tb.score
# 第二题
SELECT
student_no,
ROUND(AVG(score),2) AS avg_score
FROM
score
GROUP BY
student_no
HAVING
AVG(Score) > 60
# 第三题
SELECT
tc.`name`,
tc.student_no,
COUNT(ta.course_id),
if (SUM(score), SUM(score), 0)
FROM
score ta RIGHT JOIN student tc on ta.student_no = tc.student_no
GROUP BY ta.student_no
ORDER BY tc.student_no;
# 第四题
SELECT s.student_no,
s.`name`
FROM student s LEFT JOIN
score sc
on s.student_no = sc.student_no
GROUP BY s.student_no
HAVING COUNT(sc.course_id) < (SELECT COUNT(*) FROM course c)
ORDER BY s.student_no;
# 第五题
SELECT
student_no,
ROUND(AVG(score), 2)
FROM
score
GROUP BY
student_no
HAVING
FIND_IN_SET("3",GROUP_CONCAT(course_id)) and
FIND_IN_SET("2",GROUP_CONCAT(course_id))