建表语句
create tableStudent(sid varchar(10),snamevarchar(10),sage datetime,ssexnvarchar(10));insert into Studentvalues('01','赵雷','1990-01-01','男');insert into Studentvalues('02','钱电','1990-12-21','男');insert into Studentvalues('03','孙风','1990-05-20','男');insert into Studentvalues('04','李云','1990-08-06','男');insert into Studentvalues('05','周梅','1991-12-01','女');insert into Studentvalues('06','吴兰','1992-03-01','女');insert into Studentvalues('07','郑竹','1989-07-01','女');insert into Studentvalues('08','王菊','1990-01-20','女');create tableCourse(cid varchar(10),cnamevarchar(10),tidvarchar(10));insert into Coursevalues('01','语文','02');insert into Coursevalues('02','数学','01');insert into Coursevalues('03','英语','03');create tableTeacher(tid varchar(10),tnamevarchar(10));insert into Teachervalues('01','张三');insert into Teachervalues('02','李四');insert into Teachervalues('03','王五');create tableSC(sid varchar(10),cidvarchar(10),scoredecimal(18,1));insert into SCvalues('01','01',80);insert into SCvalues('01','02',90);insert into SCvalues('01','03',99);insert into SCvalues('02','01',70);insert into SCvalues('02','02',60);insert into SCvalues('02','03',80);insert into SCvalues('03','01',80);insert into SCvalues('03','02',80);insert into SCvalues('03','03',80);insert into SCvalues('04','01',50);insert into SCvalues('04','02',30);insert into SCvalues('04','03',20);insert into SCvalues('05','01',76);insert into SCvalues('05','02',87);insert into SCvalues('06','01',31);insert into SCvalues('06','03',34);insert into SCvalues('07','02',89);insert into SCvalues('07','03',98);
表结构预览
--学生表
Student(SId,Sname,Sage,Ssex)
--SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
--课程表
Course(CId,Cname,TId)
--CId 课程编号,Cname 课程名称,TId 教师编号
--教师表
Teacher(TId,Tname)
--TId 教师编号,Tname 教师姓名
--成绩表
SC(SId,CId,score)
--SId 学生编号,CId 课程编号,score 分数
题目
1、查询“01”课程比“02”课程成绩高的所有学生的学号;
2、查询平均成绩大于60分的同学的学号和平均成绩;
3、查询所有同学的学号、姓名、选课数、总成绩
4、查询姓“李”的老师的个数;
5、查询没学过“张三”老师课的同学的学号、姓名;
6、查询学过编号“01”并且也学过编号“02”课程的同学的学号、姓名;
7、查询学过“张三”老师所教的课的同学的学号、姓名;
8、查询课程编号“01”的成绩比课程编号“02”课程低的所有同学的学号、姓名;
9、查询所有课程成绩小于60分的同学的学号、姓名;
10、查询没有学全所有课的同学的学号、姓名;
11、查询至少有一门课与学号为“01”的同学所学相同的同学的学号和姓名;
12、查询和"01"号的同学学习的课程完全相同的其他同学的学号和姓名
13、把“SC”表中“张三”老师教的课的成绩都更改为此课程的平均成绩;
14、查询没学过"张三"老师讲授的任一门课程的学生姓名
15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
16、检索"01"课程分数小于60,按分数降序排列的学生信息
17、按平均成绩从高到低显示所有学生的平均成绩
18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率
19、按各科平均成绩从低到高和及格率的百分数从高到低顺序
20、查询学生的总成绩并进行排名
21、查询不同老师所教不同课程平均分从高到低显示
22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
24、查询学生平均成绩及其名次
25、查询各科成绩前三名的记录
26、查询每门课程被选修的学生数
27、查询出只选修了一门课程的全部学生的学号和姓名
28、查询男生、女生人数
29、查询名字中含有"风"字的学生信息
30、查询同名同性学生名单,并统计同名人数
31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime)
32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
37、查询不及格的课程,并按课程号从大到小排列
38、查询课程编号为"01"且课程成绩在60分以上的学生的学号和姓名;
40、查询选修“张三”老师所授课程的学生中,成绩最高的学生姓名及其成绩
42、查询每门功课成绩最好的前两名
43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
44、检索至少选修两门课程的学生学号
45、查询选修了全部课程的学生信息
46、查询各学生的年龄
47、查询本周过生日的学生
48、查询下周过生日的学生
49、查询本月过生日的学生
50、查询下月过生日的学生
答案
1、查询“01”课程比“02”课程成绩高的所有学生的学号;
select distinct t1.sidassidfrom(select *fromsc where cid='01')t1left join (select *fromsc where cid='02')t2on t1.sid=t2.sidwhere t1.score>t2.score
2、查询平均成绩大于60分的同学的学号和平均成绩;
select sid ,avg(score)fromscgroup by sidhaving avg(score>60)
3、查询所有同学的学号、姓名、选课数、总成绩
select student.sidassid ,sname ,count(distinct cid) course_cnt ,sum(score)astotal_scorefromstudentleft join sc on student.sid=sc.sidgroup by sid,sname
4、查询姓“李”的老师的个数;
select count(distinct tid)asteacher_cntfromteacherwhere tname like'李%'
5、查询没学过“张三”老师课的同学的学号、姓名;
select sid,snamefromstudentwhere sidnotin( select sc.sidfromteacher left join course on teacher.tid=course.tid left join sc on course.cid=sc.cid where teacher.tname='张三')
6、查询学过“01”并且也学过编号“02”课程的同学的学号、姓名;
select t.sidassid ,snamefrom( select sid ,count(if(cid='01',score,null))ascount1 ,count(if(cid='02',score,null))ascount2fromsc group by sid having count(if(cid='01',score,null))>0and count(if(cid='02',score,null))>0)tleft join student on t.sid=student.sid
7、查询学过“张三”老师所教的课的同学的学号、姓名;
select student.sid ,snamefrom( select distinct cidfromcourse left join teacher on course.tid=teacher.tid where teacher.tname='张三')courseleft join sc on course.cid=sc.cidleft join student on sc.sid=student.sidgroup by student.sid,sname
8、查询课程编号“01”的成绩比课程编号“02”课程低的所有同学的学号、姓名;
select t1.sid,snamefrom( select distinct t1.sidassidfrom(select *fromsc where cid='01')t1 left join (select *fromsc where cid='02')t2 on t1.sid=t2.sid where t1.score>t2.score )t1left join student on t1.sid=student.sid
9、查询所有课程成绩小于60分的同学的学号、姓名;
select t1.sid,snamefrom( select sid,max(score)fromsc group by sid having max(score<60) )t1left join student on t1.sid=student.sid
10、查询没有学全所有课的同学的学号、姓名;
select t1.sid,snamefrom( select count(cid),sidfromsc group by sid having count(cid) < (select count(distinct cid)fromcourse) )t1left join student on t1.sid=student.sid
11、查询至少有一门课与学号为“01”的同学所学相同的同学的学号和姓名;
select distinct sc.sidfrom( select cidfromsc where sid='01')t1left join sc on t1.cid=sc.cid
12、查询和"01"号的同学学习的课程完全相同的其他同学的学号和姓名
#注意是和'01'号同学课程完全相同但非学习课程数相同的,这里我用左连接解决这个问题select t1.sid,snamefrom( select sc.sid ,count(distinct sc.cid)from( select cidfromsc where sid='01')t1#选出01的同学所学的课程left join sc on t1.cid=sc.cid group by sc.sid having count(distinct sc.cid)= (select count(distinct cid)fromsc where sid ='01') )t1left join student on t1.sid=student.sidwhere t1.sid!='01'
13、把“SC”表中“张三”老师教的课的成绩都更改为此课程的平均成绩;
#暂跳过update题目
14、查询没学过"张三"老师讲授的任一门课程的学生姓名
select snamefromstudentwhere sidnotin( select distinct sidfromsc left join course on sc.cid=course.cid left join teacher on course.tid=teacher.tid where tname='张三')
15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select t1.sid,sname,avg_scorefrom( select sid,count(if(score<60,cid,null)),avg(score)asavg_scorefromsc group by sid having count(if(score<60,cid,null)) >=2)t1left join student on t1.sid=student.sid
16、检索"01"课程分数小于60,按分数降序排列的学生信息
select sid,if(cid='01',score,100)from scwhereif(cid='01',score,100)<60order byif(cid='01',score,100) desc
17、按平均成绩从高到低显示所有学生的平均成绩
select sid,avg(score)fromscgroup by sidorder by avg(score) desc
18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率
select sc.cid ,cname ,max(score)asmax_score ,min(score)asmin_score ,avg(score)asavg_score ,count(if(score>=60,sid,null))/count(sid)aspass_ratefromsc left join course on sc.cid=course.cidgroup by sc.cid
19、按各科平均成绩从低到高和及格率的百分数从高到低顺序
#这里先按照平均成绩排序,再按照及格百分数排序,题目有点奇怪select cid ,avg(score)asavg_score ,count(if(score>=60,sid,null))/count(sid)aspass_ratefrom scgroup by cidorder by avg_score,pass_rate desc
20、查询学生的总成绩并进行排名
select sid ,sum(score)assum_scorefromscgroup by sidorder by sum_score desc
21、查询不同老师所教不同课程平均分从高到低显示
select tid ,avg(score)asavg_scorefromcourseleft join sc on course.cid=sc.cidgroup by tidorder by avg_score desc
22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
select sid,rank_num,score,cidfrom( select rank() over(partition by cid order by score desc)asrank_num ,sid ,score ,cidfromsc )twhere rank_numin(2,3)
23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
select sc.cid ,cname ,count(if(score between85and100,sid,null))/count(sid) ,count(if(score between70and85,sid,null))/count(sid) ,count(if(score between60and70,sid,null))/count(sid) ,count(if(score between0and60,sid,null))/count(sid)from scleft join course on sc.cid=course.cidgroup by sc.cid,cname
24、查询学生平均成绩及其名次
select sid ,avg_score ,rank() over (order by avg_score desc)from( select sid ,avg(score)asavg_scorefromsc group by sid )t
25、查询各科成绩前三名的记录
select sid,cid,rank1from( select cid ,sid ,rank() over(partition by cid order by score desc)asrank1fromsc )twhere rank1<=3
26、查询每门课程被选修的学生数
select count(sid) ,cidfromscgroup by cid
27、查询出只选修了一门课程的全部学生的学号和姓名
#只查出来sid即可,后面懒得交student表select sidfromscgroup by sidhaving count(cid) =1
28、查询男生、女生人数
select ssex ,count(distinct sid)fromstudentgroup by ssex
29、查询名字中含有"风"字的学生信息
select sid,snamefrom studentwheresname like'%风%'
30、查询同名同性学生名单,并统计同名人数
#题目有歧义,这套题的质量感觉有点差select ssex ,sname ,count(sid)fromstudentgroup by ssex,snamehaving count(sid)>=2
31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime)
select sid,sname,sagefrom studentwhereyear(sage)=1990
32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
select cid,avg(score)asavg_scorefromscgroup by cidorder by avg_score,cid desc
37、查询不及格的课程,并按课程号从大到小排列
#有问题的题目select cid,sid,scorefrom scwherescore<60order by cid desc,sid
38、查询课程编号为"01"且课程成绩在60分以上的学生的学号和姓名;
select sid,cid,scorefromscwhere cid='01'andscore>60
40、查询选修“张三”老师所授课程的学生中,成绩最高的学生姓名及其成绩
select sc.sid,sname,cname,scorefrom scleft join course on sc.cid=course.cidleft join teacher on course.tid=teacher.tidleft join student on sc.sid=student.sidwheretname='张三'order by score desclimit1;
42、查询每门功课成绩最好的前两名
##感觉题目重复了select cid,sid,rank1from( select cid ,sid ,rank() over(partition by cid order by score desc)asrank1fromsc )twhere rank1 <=2
43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select cid ,count(sid)ascntfromscgroup by cidhaving cnt>=5order by count(sid) desc,cid
44、检索至少选修两门课程的学生学号
select sid ,count(cid)from scgroup by sidhavingcount(cid)>=2
45、查询选修了全部课程的学生信息
#不太严谨,但实务中应该没问题,如需严谨见12题思路select sid ,count(cid)fromscgroup by sidhaving count(cid)=(select count(distinct cid)fromsc)
46、查询各学生的年龄
select sid,sname,year(curdate())-year(sage)assagefromstudent
47、查询本周过生日的学生
select sid,sname,sagefrom studentwhereweekofyear(sage)=weekofyear(curdate())
48、查询下周过生日的学生
select sid,sname,sagefrom studentwhereweekofyear(sage) = weekofyear(date_add(curdate(),interval 1 week))
49、查询本月过生日的学生
select sid,sname,sagefrom studentwheremonth(sage) = month(curdate())
50、查询下月过生日的学生
select sid,sname,sagefrom studentwheremonth(date_sub(sage,interval 1 month)) = month(curdate())
作者:TOMOCAT
链接:https://www.jianshu.com/p/d6ca0a611fd2
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。