mysql练习题版本一

有些题还没有解决

-- 查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数 1.1 查询同时存在" 01 "课程和" 02 "课程的情况 1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null ) 1.3 查询不存在" 01 "课程但存在" 02 "课程的情况
select  from student s join (select sid,score from sc where cid=01) s1 join (select sid,score from sc where cid=02) s2
on s1.sid=s2.sid where s1.score>s2.score and(s.sid=s1.sid,s.sid=s2,sid);

-- 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select s.sid,s.sname,c.a from student s join (select sid,avg(score) a from sc group by sid) c on c.sid=s.sid and c.a>60;

-- 查询在 SC 表存在成绩的学生信息
select distinct s.* from student s join sc on sc.sid=s.sid and sc.score > 0;

-- 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) 4.1 查有成绩的学生信息
select s.sid,s.sname,c.cidSum as '选课总数',c.scoreSum as '总成绩' from student s left join 
(select sid,count(cid) as cidSum,sum(score) as scoreSum from sc group by sid) c on s.SId=c.sid;

-- 查询「李」姓老师的数量
select count(*) from teacher where Tname like '李%';

-- 查询学过「张三」老师授课的同学的信息
select s.* from student s join (select sid from sc where cid=(select cid from teacher t join course c on t.tname='张三' and t.tid=c.tid)) b on s.sid=b.sid;

-- 查询没有学全所有课程的同学的信息
select s.* from student s join (select sid,count(cid) c from sc group by sid) b on s.sid=b.sid and b.c=(select count(cid) from course);

-- 查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
select distinct s.* from student s join (select sid,cid from sc) b on b.cid in (select cid from sc where sid=01) and s.sid=b.sid;

-- 查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息      ``12`12`12`12`12`2`2`12`121`2`12
select distinct s.* from student s join (select sid,cid from sc where sid != 01) b join (select cid from sc where sid=01) d on b.cid = d.cid and s.sid=b.sid;
select distinct s.* from student s join (select sid,cid from sc where sid != 01) b on b.sid not in (select sid from sc where cid not in (select sid,cid from sc where sid != 01)) and s.sid=b.sid;
-- 查询没学过"张三"老师讲授的任一门课程的学生姓名
select distinct s.sname from student s where s.sid not in (select sid from sc where cid=(select c.cid from Teacher t join Course c on t.Tid = c.tid and t.Tname = '张三'));

-- 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select s.sid,s.sname,avg(sc.score) from student s join sc on s.sid not in (select distinct sid from sc where sid not in (select sid from sc where score<60 group by sid having count(cid)>=2)) and s.sid=sc.sid group by sid

select s.sid,s.sname,avg(sc.score) from Student s join SC on s.sid=sc.sid join (select sid,count(score) c from sc where score<60 group by sid having c>=2) a on s.sid = a.sid group by s.sid

-- 检索" 01 "课程分数小于 60,按分数降序排列的学生信息
select s.*,sc.score from student s join sc on s.sid=sc.sid and score<60 and cid=01 order by score desc;
-- 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select sc.sid,sc.cid,sc.score from sc join (select sid,avg(score) a from sc group by sid) s on  sc.sid=s.sid order by s.a desc;
-- 查询各科成绩最高分、最低分和平均分: 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select * from (select sc.cid as x,c.cname,max(sc.score) zg,min(sc.score) zd,avg(sc.score) pj from sc join Course c on sc.cid=c.cid group by sc.cid) aa join 
(select cid as x,concat(count(CASE WHEN 80>score and score>=70 then '中等' end)/count(cid)*100,'%') zd,
concat(count(CASE WHEN 90>score and score>=80 then '优良' end)/count(cid)*100,'%') yl,
concat(count(CASE WHEN 100>score and score>=90 then '优秀' end)/count(cid)*100,'%') yx,
concat(count(case when score>=60 then '及格' end)/count(cid)*100,'%') jg from sc group by sc.cid) bb on aa.x=bb.x	

(select cid,concat(count(CASE WHEN 80>score and score>=70 then '中等' end)/count(cid)*100,'%') zd,
concat(count(CASE WHEN 90>score and score>=80 then '优良' end)/count(cid)*100,'%') yl,
concat(count(CASE WHEN 100>score and score>=90 then '优秀' end)/count(cid)*100,'%') yx,
concat(count(case when score>=60 then '及格' end)/count(cid)*100,'%') jg  from sc group by cid)


,
count(case when score>=60 then '及格' end) jg 

SELECT cid,
(CASE WHEN score < 60 THEN '不及格'
WHEN score >= 60 AND score < 80 THEN '及格'
WHEN score >= 80 THEN '优秀'
ELSE '异常' END) AS REMARK
FROM sc;

-- 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 
select cid,sid,score from sc order by score desc
select cid,sid,score from sc where

SELECT *, RANK() OVER(PARTITION BY sc.cid ORDER BY sc.score DESC) 排名 FROM sc;

SELECT cid, sid, score, @curRank := @curRank + 1 AS rank
FROM sc, (
SELECT @curRank := 0
) q
ORDER BY score desc

SELECT cid, sid, score, rank FROM
(SELECT cid, sid, score,
@curRank := IF(@prevRank = score, @curRank, @incRank) AS rank, 
@incRank := @incRank + 1, 
@prevRank := score
FROM sc, (
SELECT @curRank :=0, @prevRank := NULL, @incRank := 1
) r 
ORDER BY score) s 
-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次
-- SELECT *, DENSE_RANK() OVER(PARTITION BY sc.cid) ORDER BY sc.score DESC)排名 FROM sc; 
-- 参考答案 但是不能使用 好像因为这里使用的函数是oracle里面的
select sid,cid,score,@rank:=@rank+1 as rank from sc join (select @rank:=0) q order by score desc

-- 查询学生的总成绩,并进行排名,总分重复时保留名次空缺 
select sid,sum(score), (case when )  from sc join (select @rank:=0) a group by sid ORDER BY sum(score) desc;
-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺
select sid,sum(score),@rank:=@rank+1 as rank from sc join (select @rank:=0) a group by sid ORDER BY sum(score) desc;
-- 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
-- 查询各科成绩前三名的记录

-- 查询每门课程被选修的学生数
select cid,count(sid) from sc group by cid;
-- 查询出只选修两门课程的学生学号和姓名
select s.sid,s.sname from student s join (select sid,count(cid) c from sc group by sid) d on s.sid=d.sid and d.c=2;
-- 查询男生、女生人数
select ssex,count(ssex) as '人数'from student group by Ssex;
-- 查询名字中含有「风」字的学生信息
select * from student where sname like '%风%';
-- 查询同名同性学生名单,并统计同名人数
select sname,count(*) from student group by sname having count(*)>1;
-- 查询 1990 年出生的学生名单
select * from student where Sage like '%1990%';
-- 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select cid,avg(score) from sc group by cid order by avg(score)desc,cid ASC;
-- 查询平均成绩大于等于 85 的所有学生的f学号、姓名和平均成绩
select s.sid,s.sname,avg(sc.score) from student s join sc on s.sid=sc.sid group by s.sid having avg(sc.score)>=85;
-- 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
select s.sname,sc.score from student s join sc on s.sid=sc.sid and sc.cid=(select cid from course where cname='数学') having score<60;
-- 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
select s.sid,sc.cid,sc.score from student s left join sc on s.sid=sc.sid;
-- 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
select s.sname,sc.cid,sc.score from student s join sc on s.sid=sc.sid having sc.score>70;
-- 查询不及格的课程
select cid,score from sc where score<60;
-- 查询课程编号为 01 且课程成绩在 70 分以上的学生的学号和姓名
select s.sid,s.sname,sc.score from student s join sc on sc.cid=01 and sc.sid=s.sid having score>70;
-- 求每门课程的学生人数
select cid,count(sid) from sc group by cid;
-- 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
select s.*,sc.score from student s join sc on cid=(select c.cid from course c join teacher t on t.tname='张三' and t.tid=c.tid) and s.sid=sc.sid having max(sc.score);
select s.*,sc.score from student s join sc on cid=(select c.cid from course c join teacher t on t.tname='张三' and t.tid=c.tid) and s.sid=sc.sid order by sc.score desc LIMIT 1;
-- 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
-- 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
******select s1.sid,s1.cid,s1.score from sc s1 join sc s2 on s1.cid != s2.cid and s1.score=s2.score*****
-- 查询每门功成绩最好的前两名
select sc.cid from student s join sc on s.sid=sc.sid group by cid*****
select * from sc group by cid ORDER BY score desc LIMIT 2****
-- 统计每门课程的学生选修人数(超过 5 人的课程才统计)。
select cid,count(sid) 人数 from sc group by cid having count(sid)>5;
-- 检索至少选修两门课程的学生学号
select sid,count(cid) from sc group by sid having count(cid)>=2;
-- 查询选修了全部课程的学生信息
select s.* from student s join 
(select sid from sc group by sid having count(cid)=(select count(cid) from course)) a on s.sid=a.sid;
-- 查询各学生的年龄,只按年份来算
select sname,(year(CURRENT_DATE)-YEAR(Sage)) as age from student;
SELECT SId, Sname, (year(curdate())-year(Sage)) as age FROM student ORDER BY age;
-- 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
-- 查询本周过生日的学生
select * from student where week(CONCAT(YEAR(CURRENT_DATE),mid(sage,5,6)))=week(CURRENT_DATE);

select week(CONCAT(YEAR(CURRENT_DATE),mid(sage,5,6))) from student where sname='孙七'
SELECT week(CURRENT_DATE)
-- 查询下周过生日的学生
select * from student where week(CONCAT(YEAR(CURRENT_DATE),mid(sage,5,6)))=week(CURRENT_DATE)+1;
-- 查询本月过生日的学生
select * from student where MONTH(sage)=MONTH(CURRENT_DATE);
-- 查询下月过生日的学生 
select * from student where MONTH(sage)=MONTH(CURRENT_DATE)+1;
posted @   没有烦恼的猫猫  阅读(87)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
点击右上角即可分享
微信分享提示