mysql经典sql笔试题
百度云:表含数据 提取码:9psf
Mysql 版本 5.7.13
1、查询“01”课程比“02”课程成绩高的所有学生的学号和姓名:
select * from student where sid in
(select s1.sid from (select * from sc where cid="01")s1
left join (select * from sc where cid="02" ) s2
on s1.sid = s2.sid
where s1.score > s2.score);
2、查询平均成绩大于60分的同学的学号和平均成绩;
select avg(score) sco from sc group by sid having sco > 60
3、查询所有同学的学号、姓名、选课数、总成绩
select st.sname,st.sid,count(s.cid),count(s.sid),sum(score)
from student st left join sc s on st.sid = s.sid
GROUP BY s.sid
4、查询姓“李”的老师的个数;
select count(tid) from teacher where tname like "%李%"
5、查询没学过“张三”老师课的同学的学号、姓名;
select * from student stu
where stu.sid not in
(select sid from sc s inner join
(select cid from teacher t
left join course c on t.tid = c.tid
where t.tname= "张三") c on s.cid = c.cid) ;
6、查询学过编号“01”并且也学过编号“02”课程的同学的学号、姓名;
select * from student st inner join
(select s1.sid from sc s1 inner join sc s2 on s1.sid = s2.sid where s1.cid = 01 and s2.cid = 02)res
on st.sid = res.sid;
#另一种解法
SELECT t.sid AS sid,student.sname
FROM (SELECT sid FROM sc GROUP BY sid HAVING COUNT(IF(cid = "01",score,NULL)) > 0 AND COUNT(IF(cid = "02",score,NULL)) > 0) AS t
LEFT JOIN student ON t.sid = student.sid;
7、查询学过“张三”老师所教的课的同学的学号、姓名;
select stu.sid,stu.sname from student stu inner join (select sid from sc s inner join
( select cid from teacher t left join course c on t.tid = c.tid where t.tname= "张三") c on s.cid = c.cid) res on stu.sid= res.sid ;
8、查询课程编号“01”的成绩比课程编号“02”课程低的所有同学的学号、姓名;
select st.sid,st.sname
from student st inner join (select s1.sid from sc s1 inner join sc s2 on s1.sid = s2.sid where s1.cid = 01 and s2.cid = 02 and s1.score < s2.score)res
on st.sid = res.sid;
9、查询所有课程成绩小于60分的同学的学号、姓名;
select st.sid,st.sname
from student st inner join
(select sid from sc group by sid having count(if(score<60,1,NULL))>0)res
on st.sid = res.sid;
10、查询没有学全所有课的同学的学号、姓名;
select st.sid,st.sname
from student st inner join
(select sid from sc group by sid having count(cid) < (select count(cid) from course)) res
on st.sid = res.sid
11、查询至少有一门课与学号为“01”的同学所学相同的同学的学号和姓名;
select st.sid,st.sname from student st
inner join (SELECT distinct c2.sid FROM (SELECT cid FROM sc WHERE sid = "01") c1 LEFT JOIN (SELECT sid,cid FROM sc WHERE sid != "01") c2 ON c1.cid = c2.cid ) res
on st.sid = res.sid
12、查询和"01"号的同学学习的课程完全相同的其他同学的学号和姓名
select st.sid,st.sname
from student st inner join (select c2.sid from (SELECT GROUP_CONCAT(cid) res FROM sc WHERE sid = "01") c1
inner JOIN (SELECT GROUP_CONCAT(cid) res,sid FROM sc WHERE sid != "01" group by sid ) c2 ON c1.res = c2.res) res
on st.sid = res.sid
13、把“SC”表中“张三”老师教的课的成绩都更改为此课程的平均成绩
update sc s set score =(select * from ( select avg(score) from sc where cid=
(select c.cid from teacher t inner join course c on t.tid = c.tid where tname = "张三")) res)
where cid = (select c.cid from teacher t inner join course c on t.tid = c.tid where tname = "张三" )
14、查询没学过"张三"老师讲授的任一门课程的学生姓名
select st.sid,st.sname
from student st inner join (select * from sc group by sid having
count(if((select c.cid from teacher t inner join course c on t.tid = c.tid where tname = "张三")=cid,1,NULL))=0)res
on st.sid = res.sid
15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select st.sid,st.sname ,res.avgScore
from student st inner join
(select sid,avg(score)avgScore from sc group by sid having count(if(score<=60,1,NULL))>=2 )res
on st.sid = res.sid
16、检索"01"课程分数小于60,按分数降序排列的学生信息
select st.* from student st
inner join (select sid from sc where cid = 01 and score <60 order by score desc)res
on st.sid = res.sid
17、按平均成绩从高到低显示所有学生的平均成绩
select st.sid,st.sname,avg(s.score)avgScore
from student st left join sc s on st.sid = s.sid
group by st.sid order by avgScore desc
18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率
select s.cid 课程ID ,c.cname 课程name ,max(s.score) 最高分,min(s.score)最低分,avg(s.score)平均分,
count(if(s.score > 60,s.sid,NULL)) / COUNT(s.sid) 及格率
from sc s inner join course c on s.cid = c.cid
group by s.cid
19、按各科平均成绩从低到高和及格率的百分数从高到低顺序
select avg(score)score,cid, COUNT(IF(score > 60,sid,NULL)) / COUNT(sid) passRate
from sc group by cid order by score asc,passRate DESC
20、查询学生的总成绩并进行排名
select res.score,stu.sname from student stu inner join
(select sum(score) score,sid from sc group by sid )res
on stu.sid = res.sid
order by res.score desc;
21、查询不同老师所教不同课程平均分从高到低显示
select avg(score)score,cid from sc group by cid order by score desc
22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[84-70],[69-60],[0-60]及所占百分比
select c.cid, c.cname,
ROUND(sum(case when s.score<=100 and s.score>=85 then 1 else 0 end) / count(s.cid) *100,2) as '[100-85]',
ROUND(sum(case when s.score<=84 and s.score>=70 then 1 else 0 end) / count(s.cid) *100,2)as '[84-70]',
ROUND(sum(case when s.score<=69 and s.score>=60 then 1 else 0 end) / count(s.cid) *100,2)as '[69-60]',
ROUND(sum(case when s.score<60 then 1 else 0 end) / count(s.cid) *100,2)as '[0-60]'
from sc s inner join course c on s.cid = c.cid
group by s.cid
24、查询学生平均成绩及其名次
select @rownum := @rownum+1 as num,avg(s.score) score from sc s,(select @rownum := 0)temp
group by s.sid order by s.score desc
25、查询各科成绩前三名的记录
26、查询每门课程被选修的学生数
select count(sid) num from sc group by cid
27、查询出只选修一门课程的全部学生的学号和姓名
select count(sid)cou,sid from sc group by sid having cou<=1
28、查询男生,女生人数
select
sum(case ssex when '男' then 1 else 0 end) '男',
sum(case ssex when '女' then 1 else 0 end) '女'
from student
29、查询名字中含有“风”字的学生信息
select * from student where sname like "%风%"
30、查询同名同性学生名单,并统计同名人数
select sname,count(1) from student group by sname having count(1) > 1
31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime)
select * from student where year(sage)=1990
32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
select avg(score)score,cid
from sc group by cid order by score asc, cid desc
33、查询课程编号为“ 01”且课程成绩在60分以上的学生的学号和姓名;
select * from student s inner join
(select sid from sc where cid = 01 and score >60) res
on s.sid = res.sid
34、查询选修“张三”老师所授课程的学生中,成绩最高的学生姓名及其成绩
select sname,res.score from student s inner join
(select sid,score from sc where cid =
(select c.cid from teacher t,course c where t.tname = "张三"and c.tid=t.tid)
order by score desc limit 0,1)res on res.sid = s.sid
35、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select cid,COUNT(cid)sum from sc group by cid having sum > 5 order by sum desc ,cid asc
36、检索至少选修两门课程的学生学号
select sid from sc group by sid having count(sid) >= 2
37、查询选修了全部课程的学生信息
select * from sc group by sid having COUNT(sid) = (select count(cid) from course)