python/MySQL练习题(二)

时间:2021-04-03 19:15:15

python/MySQL练习题(二)

查询各科成绩前三名的记录:(不考虑成绩并列情况)

  select score.sid,score.course_id,score.num,T.first_num,T.second_num from score left join
(
select
sid,
(select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 0,1) as first_num,
(select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 3,1) as second_num
from
score as s1
) as T
on score.sid =T.sid
where score.num <= T.first_num and score.num >= T.second_num

查询每门课程被选修的学生数

 SELECT count(student_id) from score LEFT JOIN course on course.cid=score.course_id
GROUP BY course_id
HAVING COUNT(1) >4

查询出只选修了一门课程的全部学生的学号和姓名

 SELECT student.sid,student.sname from score LEFT JOIN student on student.sid=score.student_id
LEFT JOIN course on course.cid=score.course_id
GROUP BY student_id
HAVING count(score.course_id)=''

查询男生、女生的人数

 SELECT gender,count(sid) from student
GROUP BY gender
HAVING count(sid)

查询姓“张”的学生名单

 SELECT * from student where sname like '张%'

查询同名同姓学生名单,并统计同名人数

 SELECT sname,COUNT(sname) from student
GROUP BY sname
HAVING COUNT(sname)

查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列

  SELECT course_id,avg(num) from score LEFT JOIN course on course.cid=score.course_id
GROUP BY course_id
ORDER BY avg(num) asc

查询平均成绩大于85的所有学生的学号、姓名和平均成绩

  SELECT student.sid,student.sname,avg(num) from score LEFT JOIN student on student.sid=score.student_id
GROUP BY student_id
HAVING avg(num) > 85

查询课程名称为“物理”,且分数低于60的学生姓名和分数

 SELECT student.sname,score.num  from score LEFT JOIN student on student.sid=score.student_id
LEFT JOIN course on course.cid=score.course_id
where course.cname='生物' and score.num <60

查询课程编号为003且课程成绩在80分以上的学生的学号和姓名

 SELECT student.sid,student.sname from score LEFT JOIN course on course.cid=score.course_id
LEFT JOIN student on student.sid=score.student_id
where course.cid='' and num > 80

求选了课程的学生人数

 SELECT COUNT(c) from
(SELECT count(student_id)as c from score GROUP BY student_id)as A

查询选修“李平”老师所授课程的学生中,成绩最高的学生姓名及其成绩

 SELECT sname,num from score
LEFT JOIN course on course.cid=score.course_id
LEFT JOIN student on student.sid=score.student_id
LEFT JOIN teacher on teacher.tid=course.teacher_id
where teacher.tname='李平老师'
GROUP BY student_id
ORDER BY num DESC
LIMIT 1

查询各个课程及相应的选修人数

 SELECT cname,COUNT(1)from score LEFT JOIN course on course.cid=score.course_id
GROUP BY course_id

查询不同课程但成绩相同的学生的学号、课程号、学生成绩

 select A1.student_id,A1.course_id,A2.course_id,A1.num,A2.num from score as A1 ,score as A2
where A1.course_id!=A2.course_id and A1.num=A2.num
GROUP BY student_id

查询每门课程成绩最好的前两名

    select score.sid,score.course_id,score.num,T.first_num,T.second_num from score left join
(
select
sid,
(select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 0,1) as first_num,
(select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 1,1) as second_num
from
score as s1
) as T
on score.sid =T.sid
where score.num <= T.first_num and score.num >= T.second_num

检索至少选修两门课程的学生学号

 -- SELECT count(A.c) from (select count(1)as c from score GROUP BY course_id)as A
SELECT student_id from score LEFT JOIN student on student.sid=score.student_id
GROUP BY student_id
HAVING count(1) > 2

查询全部学生都选修的课程的课程号和课程名

 select course_id from score GROUP BY course_id HAVING COUNT(1)=(select count(1) from student)

查询没学过“李平”老师讲授的任一门课程的学生姓名

 SELECT
student.sname
FROM
student
WHERE
sid NOT IN (
SELECT
course_id
FROM
score
LEFT JOIN course ON course.cid = score.course_id
LEFT JOIN student ON student.sid = score.student_id
LEFT JOIN teacher ON teacher.tid = course.teacher_id
WHERE
teacher.tname = '李平老师'
)

查询两门以上不及格课程的同学的学号及其平均成绩

 SELECT student_id,avg(num) from score where num <60 GROUP BY student_id HAVING count(1)>2

检索“004”课程分数小于60,按分数降序排列的同学学号

 SELECT student_id from score where course_id=4 and num <60 ORDER BY num DESC

删除“002”同学的“001”课程的成绩

 DELETE from score where student_id=2 and course_id=1