python/MySQL练习题(二)
21、查询各科成绩前三名的记录:(不考虑成绩并列情况)
1 select score.sid,score.course_id,score.num,T.first_num,T.second_num from score left join 2 ( 3 select 4 sid, 5 (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 0,1) as first_num, 6 (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 3,1) as second_num 7 from 8 score as s1 9 ) as T 10 on score.sid =T.sid 11 where score.num <= T.first_num and score.num >= T.second_num
22、查询每门课程被选修的学生数
1 SELECT count(student_id) from score LEFT JOIN course on course.cid=score.course_id 2 GROUP BY course_id 3 HAVING COUNT(1) >4
23、查询出只选修了一门课程的全部学生的学号和姓名
1 SELECT student.sid,student.sname from score LEFT JOIN student on student.sid=score.student_id 2 LEFT JOIN course on course.cid=score.course_id 3 GROUP BY student_id 4 HAVING count(score.course_id)=‘1‘
24、查询男生、女生的人数
1 SELECT gender,count(sid) from student 2 GROUP BY gender 3 HAVING count(sid)
25、查询姓“张”的学生名单
1 SELECT * from student where sname like ‘张%‘
26、查询同名同姓学生名单,并统计同名人数
1 SELECT sname,COUNT(sname) from student 2 GROUP BY sname 3 HAVING COUNT(sname)
27、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
1 SELECT course_id,avg(num) from score LEFT JOIN course on course.cid=score.course_id 2 GROUP BY course_id 3 ORDER BY avg(num) asc
28、查询平均成绩大于85的所有学生的学号、姓名和平均成绩
1 SELECT student.sid,student.sname,avg(num) from score LEFT JOIN student on student.sid=score.student_id 2 GROUP BY student_id 3 HAVING avg(num) > 85
29、查询课程名称为“物理”,且分数低于60的学生姓名和分数
1 SELECT student.sname,score.num from score LEFT JOIN student on student.sid=score.student_id 2 LEFT JOIN course on course.cid=score.course_id 3 where course.cname=‘生物‘ and score.num <60
30、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名
1 SELECT student.sid,student.sname from score LEFT JOIN course on course.cid=score.course_id 2 LEFT JOIN student on student.sid=score.student_id 3 where course.cid=‘3‘ and num > 80
31、求选了课程的学生人数
1 SELECT COUNT(c) from 2 (SELECT count(student_id)as c from score GROUP BY student_id)as A
32、查询选修“李平”老师所授课程的学生中,成绩最高的学生姓名及其成绩
1 SELECT sname,num from score 2 LEFT JOIN course on course.cid=score.course_id 3 LEFT JOIN student on student.sid=score.student_id 4 LEFT JOIN teacher on teacher.tid=course.teacher_id 5 where teacher.tname=‘李平老师‘ 6 GROUP BY student_id 7 ORDER BY num DESC 8 LIMIT 1
33、查询各个课程及相应的选修人数
1 SELECT cname,COUNT(1)from score LEFT JOIN course on course.cid=score.course_id 2 GROUP BY course_id
34、查询不同课程但成绩相同的学生的学号、课程号、学生成绩
1 select A1.student_id,A1.course_id,A2.course_id,A1.num,A2.num from score as A1 ,score as A2 2 where A1.course_id!=A2.course_id and A1.num=A2.num 3 GROUP BY student_id
35、查询每门课程成绩最好的前两名
1 select score.sid,score.course_id,score.num,T.first_num,T.second_num from score left join 2 ( 3 select 4 sid, 5 (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 0,1) as first_num, 6 (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 1,1) as second_num 7 from 8 score as s1 9 ) as T 10 on score.sid =T.sid 11 where score.num <= T.first_num and score.num >= T.second_num
36、检索至少选修两门课程的学生学号
1 -- SELECT count(A.c) from (select count(1)as c from score GROUP BY course_id)as A 2 SELECT student_id from score LEFT JOIN student on student.sid=score.student_id 3 GROUP BY student_id 4 HAVING count(1) > 2
37、查询全部学生都选修的课程的课程号和课程名
1 select course_id from score GROUP BY course_id HAVING COUNT(1)=(select count(1) from student)
38、查询没学过“李平”老师讲授的任一门课程的学生姓名
1 SELECT 2 student.sname 3 FROM 4 student 5 WHERE 6 sid NOT IN ( 7 SELECT 8 course_id 9 FROM 10 score 11 LEFT JOIN course ON course.cid = score.course_id 12 LEFT JOIN student ON student.sid = score.student_id 13 LEFT JOIN teacher ON teacher.tid = course.teacher_id 14 WHERE 15 teacher.tname = ‘李平老师‘ 16 )
39、查询两门以上不及格课程的同学的学号及其平均成绩
1 SELECT student_id,avg(num) from score where num <60 GROUP BY student_id HAVING count(1)>2
40、检索“004”课程分数小于60,按分数降序排列的同学学号
1 SELECT student_id from score where course_id=4 and num <60 ORDER BY num DESC
41、删除“002”同学的“001”课程的成绩
1 DELETE from score where student_id=2 and course_id=1
时间: 2024-10-13 00:05:34