MYSQL练习题(45题)
建表语句
-- 学生表 Student create table Student(SId varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10)); insert into Student values('01' , '赵雷' , '1990-01-01' , '男'); insert into Student values('02' , '钱电' , '1990-12-21' , '男'); insert into Student values('03' , '孙风' , '1990-05-20' , '男'); insert into Student values('04' , '李云' , '1990-08-06' , '男'); insert into Student values('05' , '周梅' , '1991-12-01' , '女'); insert into Student values('06' , '吴兰' , '1992-03-01' , '女'); insert into Student values('07' , '郑竹' , '1989-07-01' , '女'); insert into Student values('09' , '张三' , '2017-12-20' , '女'); insert into Student values('10' , '李四' , '2017-12-25' , '女'); insert into Student values('11' , '李四' , '2017-12-30' , '女'); insert into Student values('12' , '赵六' , '2017-01-01' , '女'); insert into Student values('13' , '孙七' , '2018-01-01' , '女'); 科目表 Course create table Course(CId varchar(10),Cname nvarchar(10),TId varchar(10)); insert into Course values('01' , '语文' , '02'); insert into Course values('02' , '数学' , '01'); insert into Course values('03' , '英语' , '03'); 教师表 Teacher create table Teacher(TId varchar(10),Tname varchar(10)); insert into Teacher values('01' , '张三'); insert into Teacher values('02' , '李四'); insert into Teacher values('03' , '王五'); 成绩表 SC create table SC(SId varchar(10),CId varchar(10),score decimal(18,1)); insert into SC values('01' , '01' , 80); insert into SC values('01' , '02' , 90); insert into SC values('01' , '03' , 99); insert into SC values('02' , '01' , 70); insert into SC values('02' , '02' , 60); insert into SC values('02' , '03' , 80); insert into SC values('03' , '01' , 80); insert into SC values('03' , '02' , 80); insert into SC values('03' , '03' , 80); insert into SC values('04' , '01' , 50); insert into SC values('04' , '02' , 30); insert into SC values('04' , '03' , 20); insert into SC values('05' , '01' , 76); insert into SC values('05' , '02' , 87); insert into SC values('06' , '01' , 31); insert into SC values('06' , '03' , 34); insert into SC values('07' , '02' , 89); insert into SC values('07' , '03' , 98);
1.查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数 SELECT a.* ,b.score 01课程成绩,c.score 02课程成绩 from student a ,sc b ,sc c where a.sid=b.sid and a.sid=c.sid and b.cid='01' and c.cid='02' and b.score>c.score 2.查询学过「张三」老师授课的同学的信息 SELECT a.* FROM student a JOIN sc b on a.sid=b.sid where b.cid in( SELECT cid from course where cid in( SELECT tid from teacher where Tname='张三')) 3.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 -- 参考网友代码 SELECT s.* FROM student s LEFT JOIN ( SELECT sid,GROUP_CONCAT(cid) gc FROM sc GROUP BY sid) gs ON s.sid = gs.sid WHERE gs.gc = ( SELECT GROUP_CONCAT(cid) FROM sc WHERE sid =01) AND s.sid != 01;
5.查询各科成绩最高分、最低分和平均分 SELECT b.cname,max(score),min(score),avg(score) from sc a LEFT JOIN course b on a.cid=b.cid GROUP BY a.cid,b.cname 6.查询各学生的总成绩,按总成绩降序排列 SELECT sid ,sum(score) as 总成绩 from sc GROUP BY sid ORDER BY 总成绩 desc 7.查询每门课程被选修的学生数 select a.CId,b.cname,count(a.cid) from sc a LEFT JOIN course b on a.cid=b.cid GROUP BY a.cid,b.cname; 8.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 SELECT b.sname,a.score from sc a JOIN student b on a.sid=b.sid where cid in( SELECT cid from course WHERE cname='数学') and a.score<60 9.查询有不及格数据的课程名称 SELECT a.*,b.cname from sc a left JOIN course b on a.cid=b.cid where a.score<60 10.求每门课程选修的的学生人数 select a.CId,b.cname,count(a.cid) from sc a LEFT JOIN course b on a.cid=b.cid GROUP BY a.cid,b.cname; 11.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 SELECT a.*,b.score from student a left join sc b on a.sid=b.sid where b.cid in (SELECT cid from course c,teacher d where c.tid=d.tid and d.tname= '张三') and b.score in (select DISTINCT MAX(score) from sc where cid in( SELECT cid FROM course e LEFT JOIN teacher f on e.tid=f.tid where f.tname='张三')) 12.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 SELECT a.*,b.score from student a left join sc b on a.sid=b.sid where b.cid in (SELECT cid from course c,teacher d where c.tid=d.tid and d.tname= '张三') and b.score in (select MAX(score) from sc where cid in( SELECT cid FROM course e LEFT JOIN teacher f on e.tid=f.tid where f.tname='张三')) 13.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 SELECT DISTINCT a.* from sc a,sc b where a.cid!=b.cid and a.score = b.score 14.检索至少选修两门课程的学生学号 SELECT sid , count(*) c FROM sc GROUP BY sc.sId HAVING c >=2 15.查询选修了全部课程的学生信息 SELECT * from student WHERE sid in (SELECT sid from sc GROUP BY sid HAVING count(*) =(SELECT count(*) from course )) 16.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 SELECT MONTH(NOW()) from student SELECT year(now())-year(Sage)-1 年龄减一,student.* from student where MONTH(NOW())-month(sage)<0 and day(NOW())-day(sage)<0 SELECT year(now())-year(Sage) 年龄减一,student.* from student where 17.查询本周过生日的学生 SELECT * from student where WEEK(DATE_FORMAT(now(),'%y%M%d'))=WEEK(sage) 18.查询下周过生日的学生 SELECT * from student where week(DATE_FORMAT(NOW(),'%y%m%d'))+1 =WEEK(sage); 19.查询教师所教课程且选修人数多于5人的教师姓名 SELECT a.tname from teacher a where tid in ( SELECT tid from course where cid in (SELECT cid from sc GROUP BY cid having count(*)>5)) 20 查询选修“语文”课程的男同学的成绩 SELECT a.score,b.sname from sc a LEFT JOIN student b on a.sid =b.sid where b.ssex='男' and cid in(SELECT cid from course where cname='语文')
1.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 select b.SId,b.Sname,AVG(a.score) avgscore from student b left join sc a on b.SId = a.SId GROUP BY b.SId,b.Sname HAVING avgscore >=60 2.查询在 SC 表存在成绩的学生信息 select * from student where sid in (select sid from sc) 3.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) select a.SId,a.Sname,count(b.CId) 选课总数,sum(b.score) 课程的总成绩 from student a left join sc b on a.SId=b.SId GROUP BY a.SId,a.Sname; 4.查询「李」姓老师的数量 SELECT count(*) 李姓老师的数量 from teacher where Tname like '李%' 5.查询没有学全所有课程的同学的信息 select * from student join ( select count(cid) 选修课程数,sid from sc group by sid HAVING count(cid)<( select count(cid) from course)) t where student.SId=t.sid union select s.*, 0 选修课程数,sid from student s where sid not in (SELECT sid from sc); 6.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 SELECT * from student s where s.SId in( SELECT sc.SId from sc WHERE sc.CId in( SELECT sc2.CId from sc sc2 where sc2.SId='01') ) 7.查询没学过"张三"老师讲授的任一门课程的学生姓名 select * from student c where c.SId not in( select a.SId from student a join sc b on a.SId=b.SId where b.CId in( select a.CId from course a join teacher b on a.TId = b.TId where Tname ='张三')); 8.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 select * from (select avg(score) 平均成绩,sid from sc group by sid ) t1 , (select count(*) 不及格课程数,sid from sc where score< 60 group by sid HAVING count(*)>=2) t2 where t1.sid=t2.sid 9.检索" 01 "课程分数小于 60,按分数降序排列的学生信息 select * from student join (select * from sc where score<60 and cid='01' order by score desc) t on student.sid=t.sid; 10.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 select * from student s left join ( select sc.sid,sc.cid,sc.score, 平均成绩 from sc join (select avg(score) 平均成绩,sid from sc group by sid ) t on sc.SId= t.sid order by 平均成绩 desc ) t2 on s.sid=t2.sid 11.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 select total, CONCAT(s1/total*100,"%") '0-59 占比' , CONCAT(s2/total*100,"%") '60-70 占比' , CONCAT(s3/total*100,"%") '71-85 占比' , CONCAT(s4/total*100,"%") '86-100 占比' from course c1, (select * from (select count(*) total ,CId from sc group by cid) t0 left join ( select count(*) 's1',CId c1 from sc where sc.score BETWEEN 0 and 59 group by cid ) t1 on t0.cid=t1.c1 left join ( select count(*) 's2',CId c2 from sc where sc.score BETWEEN 60 and 70 group by cid ) t2 on t0.cid=t2.c2 left join ( select count(*) 's3',CId c3 from sc where sc.score BETWEEN 71 and 85 group by cid ) t3 on t0.cid=t3.c3 left join ( select count(*) 's4',CId c4 from sc where sc.score BETWEEN 86 and 100 group by cid ) t4 on t0.cid=t4.c4 ) ts where c1.CId=ts.cid 12.查询各科成绩前三名的记录 select * from student join ( select sc.sid,sc.score,sc.cid from sc join ( select max(score) maxscore ,cid from sc GROUP BY cid ) t where sc.CId=t.cid and sc.score=t.maxscore) t2 on student.sid=t2.sid 13.查询出只选修两门课程的学生学号和姓名 SELECT SId,Sname from student where SId in( SELECT SId from sc GROUP BY SId HAVING COUNT(CId)=2); 14.查询男生、女生人数 SELECT Ssex,count(Ssex) from student GROUP BY Ssex 15.查询名字中含有「风」字的学生信息 SELECT * from student where Sname like '%风%'; 16.查询同名同性学生名单,并统计同名人数 SELECT s1.Sname, count(*) 同名人数 from student s1 JOIN student s2 on s1.SId != s2.SId and s1.Sname=s2.Sname and s1.Ssex=s2.Ssex GROUP BY s1.Sname; 17.查询 1990 年出生的学生名单 SELECT * from student where Sage like '1990%'; 18.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 SELECT CId,avg(score) avgscore from sc GROUP BY CId order by avgscore desc, CId asc 19.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 SELECT sc.SId ,s.Sname,avg(score) avgscore from sc LEFT JOIN student s on sc.SId=s.SId GROUP BY sc.SId,s.Sname having avgscore>=85; 20.查询各学生的年龄,只按年份来算 SELECT year(now())-year(Sage),stu .* from student 21.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况也要罗列出来) SELECT * from student LEFT JOIN sc on stuednt.SId=sc.SId; 22.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 SELECT s.Sname, c.Cname,sc.score FROM course c LEFT JOIN sc on c.CId=sc.CId LEFT JOIN student s on s.SId=sc.SId where sc.score>=70; 23.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 select * from student join ( select sid,score from sc where score>80 and cid='01') t on student.SId= t.sid 24.查询每门功成绩最好的前两名 SELECT sc.* from sc ,( select a.sid,a.cid from sc as a left join sc as b on a.cid = b.cid and a.score<b.score group by a.cid, a.sid having count(b.CId)<2) t where sc.sid=t.sid and sc.cid=t.cid order by sc.cid; 25.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 select CId,count(*) as countS from sc GROUP BY CId HAVING countS>5 ORDER BY countS,CId ASC