python/MySQL练习题(二)

python/MySQL练习题(二)

查询各科成绩前三名的记录:(不考虑成绩并列情况)
 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
查询每门课程被选修的学生数
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
查询出只选修了一门课程的全部学生的学号和姓名
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'
查询男生、女生的人数
1 SELECT gender,count(sid) from student
2 GROUP BY gender
3 HAVING count(sid)
查询姓“张”的学生名单
1 SELECT * from student where sname like '张%'
查询同名同姓学生名单,并统计同名人数
1 SELECT sname,COUNT(sname) from student
2 GROUP BY sname
3 HAVING COUNT(sname)
查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
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 
查询平均成绩大于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
查询课程名称为“物理”,且分数低于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
查询课程编号为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
求选了课程的学生人数
1 SELECT COUNT(c) from
2 (SELECT count(student_id)as c from score GROUP BY student_id)as A
查询选修“李平”老师所授课程的学生中,成绩最高的学生姓名及其成绩
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
查询各个课程及相应的选修人数
1 SELECT cname,COUNT(1)from score LEFT JOIN course on course.cid=score.course_id
2 GROUP BY course_id
查询不同课程但成绩相同的学生的学号、课程号、学生成绩
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
查询每门课程成绩最好的前两名
 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
检索至少选修两门课程的学生学号
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
查询全部学生都选修的课程的课程号和课程名
1 select course_id from score GROUP BY course_id HAVING COUNT(1)=(select count(1) from student)
查询没学过“李平”老师讲授的任一门课程的学生姓名
 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     )
查询两门以上不及格课程的同学的学号及其平均成绩
1 SELECT student_id,avg(num) from score where num <60 GROUP BY student_id HAVING count(1)>2
检索“004”课程分数小于60,按分数降序排列的同学学号
1 SELECT student_id from score where course_id=4 and num <60 ORDER BY num DESC
删除“002”同学的“001”课程的成绩
1 DELETE from score where student_id=2 and course_id=1

 



 

转载于:https://www.cnblogs.com/guobaoyuan/p/6985184.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值