综合练习题
表结构
整合一下方便查看
teacher
student
course
scors
练习题
1、自行创建测试数据
create table student( sid int primary key auto_increment, sname char(32), gender enum("女","男"), class_id int, constraint fk_class_id foreign key (class_id) references class(cid) on DELETE cascade on update cascade ); create table teacher( tid int primary key auto_increment, tname char(32) ); create table class( cid int primary key auto_increment, caption char(32) ); create table course( cid int primary key auto_increment, cname char(32), teacher_id int, constraint fk_teacher_id foreign key (teacher_id) references teacher(tid) on delete cascade on update cascade ); create table score( sid int primary key auto_increment, student_id int, corse_id int, number int, constraint fk_student_id foreign key (student_id) references student(sid) on delete cascade on update cascade, constraint fk_corse_id foreign key (corse_id) references score(sid) on delete cascade on update cascade );
2、查询“体育”课程比“数学”课程成绩高的所有学生的学号;
select a.student_id from (select * from score where corse_id = (select cid from course where cname = '数学'))a inner join (select * from score where corse_id = (select cid from course where cname = '体育'))b on a.student_id = b.student_id and a.number > b.number;
select a.student_id from (select score.student_id, score.number from score left join course c on score.corse_id = c.cid where c.cname = '体育') a inner join (select score.student_id, score.number from score left join course c on score.corse_id = c.cid where c.cname = '数学') b on a.student_id = b.student_id and a.number < b.number;
3、查询平均成绩大于60分的同学的学号和平均成绩;
select student_id, AVG(number)AS avg from score group by student_id having avg > 60;
4、查询所有同学的学号、姓名、选课数、总成绩;
select student.sid, sname, count(*),sum(number) from student left join score s on student.sid = s.student_id group by student.sid;
5、查询姓“长”的老师的个数;
select count(*) from teacher where tname like "长%" ;
6、查询没学过“教授”老师课的同学的学号、姓名;
select s.sid,s.sname from score inner join student s on score.student_id = s.sid inner join course c on score.corse_id = c.cid inner join teacher t on c.teacher_id = t.tid where t.tname <> '教授' group by s.sid
7、查询学过“1”并且也学过编号“2”课程的同学的学号、姓名;
select s.sid,s.sname from score inner join student s on score.student_id = s.sid inner join course c on score.corse_id = c.cid where c.cid in (1,2) group by s.sid
8、查询学过“叶平”老师所教的所有课的同学的学号、姓名;
select distinct s.sname from score left join student s on score.student_id = s.sid where corse_id not in (select cid from course left join teacher t on course.teacher_id = t.tid where tname = '教授')
9、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;
思路 1 拿到成绩表中,课程为1 的记录 2 成绩表链表学生表 作为一个记录 3 重复上面步骤拿到课程为2 的链表记录 两个记录再基于相同的学生id链表。条件设置为 成绩比较
select distinct a.sid,a.sname from (select distinct sc.number, st.sname, st.sid from score as sc inner join student st on sc.student_id = st.sid where corse_id = 2) a inner join (select distinct sc.number, st.sname, st.sid from score as sc inner join student st on sc.student_id = st.sid where corse_id = 1) b on a.sid = b.sid where a.number < b.number
10、查询有课程成绩小于60分的同学的学号、姓名;
select distinct st.sid, st.sname from student as st inner join score s on st.sid = s.student_id where s.number < 60
11、查询没有学全所有课的同学的学号、姓名;
思路
1 查询出来所有的课程然后计数
2 成绩表按照学生分组后统计记录个数
3 小于总课程个数即为没学全
4 相反 等于 则为学全
select st.sid, st.sname from student as st inner join score sc on st.sid = sc.student_id group by st.sid having count(*) < (select count(cid) from course)
12、查询至少学过学号为“001”同学所选课程中任意一门课的其他同学学号和姓名;
思路 1 先拿到 1 同学所学的所有课程的id 1.1 要链表 student 和 course 以及 score 2 将1同学的课程列表当做条件来判断是否在里面即可
2.1 因为要查其他学生,排除掉 1 同学
from student as st inner join score sc on st.sid = sc.student_id inner join course as co on sc.corse_id = co.cid where st.sid <> 1 and sc.corse_id in (select co.cid from student as st inner join score sc on st.sid = sc.student_id inner join course as co on sc.corse_id = co.cid where st.sid = 1)
13、查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名;
同上,只是不需要再排除 1 同学
14、查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;
同上上,再条件判断的时候改为完全等于
15、删除学习“长老”老师课的成绩表记录;
delete sc from score as sc inner join course as co on sc.sid = co.cid inner join teacher te on co.teacher_id = te.tid where tname = '长老'
16、向SC表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“002”课程的同学学号;②插入“002”号课程的平均成绩;
17、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;
18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
19、按各科平均成绩从低到高和及格率的百分数从高到低顺序;
20、课程平均分从高到低显示(显示任课老师);
select co.cname, te.tname, avg(sc.number) av from score as sc, course as co, teacher as te where sc.corse_id = co.cid and co.teacher_id = te.tid group by sc.corse_id order by av desc
21、查询各科成绩前三名的记录:(不考虑成绩并列情况)
22、查询每门课程被选修的学生数;
23、查询出只选修了一门课程的全部学生的学号和姓名;
24、查询男生、女生的人数;
select count(*) from student where gender ='男'; select count(*) from student where gender ='女';
25、查询姓“张”的学生名单;
select * from student where sname like '萌%'
26、查询同名同姓学生名单,并统计同名人数;
select sname,count(sname) from student group by sname
27、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
select co.cname, avg(sc.number) as av from score as sc, course as co where sc.corse_id = co.cid group by sc.corse_id order by av asc, sc.corse_id desc
28、查询平均成绩大于85的所有学生的学号、姓名和平均成绩;
select st.sname, avg(sc.number) from student as st, score as sc where st.sid = sc.student_id group by st.sid
29、查询课程名称为“数学”,且分数低于60的学生姓名和分数;
select st.sname, avg(sc.number) as av from student as st inner join score as sc on st.sid = sc.student_id inner join course as co on sc.corse_id = co.cid where co.cname = '数学' group by st.sid having av < 60
30、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;
select st.sid, st.sname from student as st inner join score sc on st.sid = sc.student_id where sc.corse_id = 3 and sc.number > 80
31、求选了编号 ‘1 ’课程的学生人数
select count(*) from student as st inner join score sc on st.sid = sc.student_id where sc.corse_id = 1
32、查询选修“长老”老师所授课程的学生中,成绩最高的学生姓名及其成绩;
select st.sid, st.sname, max(sc.number) as max from student as st, score as sc, course as co, teacher as te where sc.corse_id = co.cid and st.sid = sc.student_id and co.teacher_id = te.tid and te.tname = '长老'
33、查询各个课程及相应的选修人数;
select co.cname,count(*) from score as sc inner join course as co on sc.corse_id = co.cid group by sc.corse_id
34、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
35、查询每门课程成绩最好的前两名;
select * from score sc where 2>(select count(*) from score where sid = sc.sid and number > sc.number) order by sc.sid,sc.number desc
36、检索至少选修两门课程的学生学号;
select student.sname from score,student where student_id = student.sid group by student_id having count(*) >1
37、查询全部学生都选修的课程的课程号和课程名;
select co.cid, co.cname from student as st, score as sc, course as co where sc.corse_id = co.cid and st.sid = sc.student_id group by sc.corse_id having count(*) = (select count(*) from student)
38、查询没学过“长老”老师讲授的任一门课程的学生姓名;
39、查询两门以上不及格课程的同学的学号及其平均成绩;
select st.sid, st.sname, avg(sc.number) as av from student as st, score as sc, course as co where sc.corse_id = co.cid and st.sid = sc.student_id and sc.number < 60 having count(*) > 2
40、检索“003”课程分数小于60,按分数降序排列的同学学号;
select st.sid from score as sc inner join course as co on sc.corse_id = co.cid inner join student as st on sc.student_id = st.sid where sc.number < 60 order by sc.number desc
41、删除“002”同学的“001”课程的成绩;
delete sc from score as sc, student as st where sc.student_id = st.sid and st.sid = 2 and sc.corse_id = 1