--11、查询没有学全所有课程的同学的信息。
分析思路:已知有的同学没有选修任何课程,那么分解一下:先查询学生选修课程<总课程数的学生信息,再使用UNION连接没有选修课程的学生信息,就得到想要的结果了。
SELECT s.* from student s where s.s_id in
(SELECT s_id from score GROUP BY s_id having count(*)<
(SELECT count(*) from course))
UNION
SELECT s.* from student s where s.s_id not in(SELECT DISTINCT s_id from score);
结果集我还记得我当时的第一想法是:先查询出选修课程等于所有课程数的同学编号,在not in。但我发现这样写对目前现有的表数据来说没有问题,但是数据表改变了,这个逻辑就错误了,我当时还修改了原数据去验证我这个写法,果然是有问题的,下面是那种写法,不知道会不会有小伙伴也有过这种写法呢:
SELECT s.* from student s where s_id not in
(SELECT s_id from score GROUP BY s_id having count(*)=(SELECT COUNT(*) from course));
结果就此表数据是一样的。
--12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
分析思路:首先要知道01同学选修了哪几门课程?再查其他同学选修课程中任意一门(ANY)在01同学所选课程中即可。
SELECT s.* from student s where s.s_id in
(SELECT DISTINCT s_id from score where c_id = ANY(
SELECT c_id from score where s_id='01') ) and s_id<>'01';
或者可以用IN.
在这里我总结一下 any、in、some、all的区别:
any,in,some,all 都是子查询关键词之一,any和all 都可以与=、>、>=、<、<=、<>结合起来使用,any关键词可以理解为“对于子查询返回的列中的任一数值,如果比较结果为true,则返回true”;all的意思是“对于子查询返回的列中的所有值,如果比较结果为true,则返回true”。语句in 与“=any”是相同的;语句some是any的别名,用法相同。
-- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
分析思路:和12题对比下,可以用all
SELECT s.* from student s where s.s_id in
(SELECT DISTINCT s_id from score where c_id = all(
SELECT c_id from score where s_id='01')) and s.s_id<>'01';
但是我查询的结果是空的显然不是我要的结果,我还没搞清楚原因,但是我想到另外一种方法:
(1)查询01同学学习的课程编号;(2)找出01没有学习的课程编号;(3)学习了01没有的课程的学生编号;(4)查询选修总课程=01同学总课程的学生编号;(5) 排除(3)的学生编号,排除01同学
SELECT * from student where s_id in
(SELECT s_id from score GROUP BY s_id having COUNT(*)=(SELECT count(*) from score where s_id='01'))
and s_id not in
(SELECT DISTINCT s_id from score where c_id not in(SELECT c_id from score where s_id='01')) and s_id<>'01';
-- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
分析思路:学生姓名--student,score表,张三老师----teacher,course表;逆向思维,先查出学过的,再查没学过的。
SELECT s_name from student where s_id not in
(SELECT DISTINCT s_id from score where c_id in
(SELECT c.c_id from course c,teacher t where c.t_id=t.t_id and t_name='张三'));
-- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
分析思路:student和score表;count()和avg()函数;group by;LEFT/RIGHT JOIN
SELECT r.s_id, s.s_name, r.avs from student s
RIGHT JOIN
(SELECT s_id,ROUND( avg(s_score),2) avs from score where s_score<60 GROUP BY s_id having count(s_id)>1)r on s.s_id=r.s_id;
--16、检索"01"课程分数小于60,按分数降序排列的学生信息
分析思路: 条件:01课程,分数小于60,排序desc;学生信息student表。
SELECT s.*,r.c_id, r.s_score from student s,
(SELECT * from score where c_id='01' and s_score<60 ORDER BY s_score desc)r
where s.s_id=r.s_id;
-- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
分析思路:avg(); order by;
SELECT sc.s_id, sc.c_id, r.avs from score sc,
(SELECT s_id,ROUND(avg(s_score),0) avs from score GROUP BY s_id)r
where sc.s_id = r.s_id ORDER BY r.avs desc;
上面的结果集看的不是很舒服,我有重写了一个查询语句:
SELECT a.s_id,
(SELECT s_score from score where a.s_id=s_id and c_id='01') as '01',
(SELECT s_score from score where a.s_id=s_id and c_id='02') as '02',
(SELECT s_score from score where a.s_id=s_id and c_id='03') as '03',
ROUND(avg(s_score),0) avs from score a GROUP BY a.s_id order by avs desc;
这个结果集相较于第一种更清楚,看起来更直观。
-- 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率:
--及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
分析思路:各个率要用到AVG(),MAX()最高;MIN()最低;还要用到case when 搜索条件函数;确定分母(总数):SELECT count(*) from score,再条件占比。
SELECT s.c_id, c.c_name, max(s_score) MA, min(s_score) MI,ROUND(avg(s_score),2) avs,
ROUND((sum(case when s.s_score>=60 then 1 else 0 end)/(SELECT count(*) from score)),2) as 及格率,
ROUND((sum(case when s.s_score>=70 and s.s_score<80 then 1 else 0 end)/(SELECT count(*) from score)),2) as 中等率,
ROUND((sum(case when s.s_score>=80 and s.s_score<90 then 1 else 0 end)/(SELECT count(*) from score)),2)as 优良率,
ROUND((sum(case when s.s_score>=90 then 1 else 0 end)/(SELECT count(*) from score)),2) as 优秀率
from score s LEFT JOIN course c on s.c_id=c.c_id GROUP BY s.c_id,c.c_name;
结果集--19、按各科成绩进行排序,并显示排名
分析思路:科目,学生分组,分数降序,自查询(虚拟表)
SELECT a.s_id, a.c_id, a.s_score, count(b.s_score)+1 as rank from score a
LEFT JOIN score b
on a.s_score<b.s_score and a.c_id=b.c_id
GROUP BY a.c_id, a.s_id, a.s_score
ORDER BY a.c_id, rank ASC;
结果集--20、查询学生的总成绩并进行排名
分析思路:总成绩降序,加一列rank,设置始排名=0,循环+1。
set @crank=0;
SELECT r.s_id, r.total,@crank :=@crank+1 as rank from (SELECT s_id, sum(s_score) total from score GROUP BY s_id order by total desc)r;
--21、查询不同老师所教不同课程平均分从高到低显示
分析思路:course和score, teacher表,avg(),group by c_id, order by。
SELECT c.t_id, t.t_name, r.c_id, r.avs from course c, teacher t,
(SELECT c_id, avg(s_score) avs from score GROUP BY c_id)r
where r.c_id=c.c_id and c.t_id=t.t_id ORDER BY r.avs desc;
-- 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
分析思路:结合19题,使用between...and 查询排名第2 到第3 的学生信息和成绩
SELECT s.*, m.c_id, m.s_score from student s,
(SELECT a.s_id as a1,a.c_id as c_id, a.s_score as s_score, count(b.s_score)+1 as rank from score a
LEFT JOIN score b
on a.s_score<b.s_score and a.c_id=b.c_id
GROUP BY a.c_id, a.s_id, a.s_score
ORDER BY a.c_id, rank asc)m
where s.s_id=m.a1 and rank BETWEEN 2 and 3;
-- 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
分析思路:和18题类似
统计各科成绩各分数段的人数:
SELECT s.c_id, c.c_name,
sum(case when s_score<60 then 1 else 0 end)as '0-60人数',
sum(case when s_score>=60 and s_score<70 then 1 else 0 end)as '60-70人数',
sum(case when s_score>=70 and s_score<85 then 1 else 0 end)as '70-85人数',
sum(case when s_score>=85 and s_score<100 then 1 else 0 end)as '85-100人数'
from score s
LEFT JOIN course c on s.c_id=c.c_id
GROUP BY s.c_id, c.c_name;
做到这部的时候我就有个疑问,1.各分数段占比是指各分数段人数占总人数的占比?2.还是所有课程各分数段占该分数段的占比?3.再或者各分数段人数占该课程的人数占比?我有了这样的疑惑,便没有写下去,我找了找网上的答案,他理解的应该是第3种情况,即结果是:
查询语句我就不写了,太长了,更何况我自己没写出来哈哈哈哈哈,(见笑了)。
--24、查询学生平均成绩及其名次
分析思路:和20题一样
set @crank=0;
SELECT r.s_id, r.avs,@crank :=@crank+1 as rank from
(SELECT s_id, avg(s_score) avs from score GROUP BY s_id ORDER BY avs desc)r;
-- 25、查询各科成绩前三名的记录
SELECT * from score where
(select count(*) from score as a
where score.c_id = a.c_id and score.s_score<a.s_score)<3
order by c_id asc, s_score desc;
--26、查询每门课程被选修的学生数
简单:SELECT c_id, count(*) from score GROUP BY c_id;
-- 27、查询出只有两门课程的全部学生的学号和姓名
分析思路:count(s_id)=2, score 和student表
SELECT s_id, s_name from student where s_id in
(SELECT s_id from score GROUP BY s_id having count(s_id)=2);
-- 28、查询男生、女生人数
考察count()用法,group by
SELECT s_sex, count(*) from student GROUP BY s_sex;
--29、查询名字中含有"风"字的学生信息
考察LIKE 用法
SELECT * from student where s_name like '%风%';
-- 30、查询同名同性学生名单,并统计同名人数
SELECT s_name, s_sex, count(*) from student where s_name in
(SELECT s_name from student GROUP BY s_name having count(s_name)>1)
GROUP BY s_name, s_sex;
没有同名同性别的总结:11、12、13、14、17、18、19、22、23、25都是重点题目,其余都是比较简单的查询语句,需要理解重点题目的逻辑,理解查询语句的编写,23题我还得再想想,希望大家能给些建议。
网友评论