【MySQL——多表查询练习】

一、加载导入数据库

【MySQL——多表查询练习】_第1张图片

二、检索数据练习

1.查询出20070102号学生所选修课程的课程号和成绩.

SELECT stu.stuid,couid,score
FROM stu
JOIN scores ON stu.stuid=scores.stuid
WHERE stu.stuid=20070102;

2.查询出20070102号学生的学号,姓名,性别,所选修的课程名,成绩.

SELECT stu.stuid,stuname,sex,couname,score
FROM stu JOIN scores ON stu.stuid=scores.stuid
JOIN course ON scores.couid=course.couid
WHERE stu.stuid=20070102;

3.查询出选修了‘英语’课程的学生学号、姓名.

SELECT stu.stuid,stuname
FROM stu JOIN scores ON stu.stuid=scores.stuid
JOIN course ON scores.couid=course.couid
WHERE couname=“英语”;

4.查询出“电子信息系”所有学生的学号、姓名、以及选修的课程名及成绩

SELECT stu.stuid,stuname,couname,score
FROM course JOIN scores ON course.couid=scores.couid
JOIN stu ON scores.stuid=stu.stuid
JOIN dep ON stu.depid=dep.depid
WHERE depname=“电子信息系”;

5.查询出所有学生的学号、姓名、以及选修的课程名及成绩.

SELECT stu.stuid,stuname,couname,score
FROM course JOIN scores ON course.couid=scores.couid
JOIN stu ON scores.stuid=stu.stuid
JOIN dep ON stu.depid=dep.depid;

6.查询 “电子信息系”且选修了“高等数学”课程的学生的学号、姓名及成绩。

SELECT stu.stuid,stuname,score
FROM course JOIN scores ON course.couid=scores.couid
JOIN stu ON scores.stuid=stu.stuid
WHERE depid=1001 AND couname=“高等数学”;

7.统计各个系的学生人数

SELECT depname,COUNT(*)
FROM stu JOIN dep ON stu.depid=dep.depid
GROUP BY dep.depid;

8.统计各门课程的平均分

SELECT couname,AVG(score)
FROM course JOIN scores ON course.couid=scores.couid
GROUP BY couname;

9.查询各个系各门课程的平均成绩

SELECT depname,couname,AVG(score)
FROM course JOIN scores ON course.couid=scores.couid
JOIN stu ON scores.stuid=stu.stuid
JOIN dep ON stu.depid=dep.depid
GROUP BY depname,couname;

10.查找选修的课程中超过2门成绩在80分以上的学生的学号和成绩高于80分的门数

SELECT stu.stuid,COUNT(score>80)
FROM stu JOIN scores ON stu.stuid=scores.stuid
WHERE score>80
GROUP BY stu.stuid
HAVING count(couid)>2;

11.查询出三好生的候选人学号,三好生候选人需要满足的条件是:其各科平均成绩不低于85分且没有不及格的科目

SELECT stu.stuid
FROM stu JOIN scores ON stu.stuid=scores.stuid
WHERE score>60
GROUP BY couid
HAVING AVG(score)>=85;

12.求“英语”课程的平均成绩

SELECT AVG(score)
FROM course JOIN scores ON course.couid=scores.couid
WHERE couname=“英语”
GROUP BY couname;

13.查询出一门课程都没有选修的学生学号和姓名

SELECT stu.stuid,stuname
FROM stu JOIN scores ON stu.stuid=scores.stuid
WHERE couid NOT IN
(SELECT DISTINCT couid FROM scores);

14.请查询出没有学生选修的课程号和课程名

SELECT course.couid,couname
FROM course JOIN scores ON course.couid=scores.couid
WHERE course.couid NOT IN
(SELECT couid FROM course);

以上仅为个人练习操作,如有错误欢迎指正!!

你可能感兴趣的:(MySQL课程学习,mysql,大数据)