45题SQL练习对应的navicat查询语句

tech2022-08-10  145

-- 学生表 create table if not exists student ( sid int(10) comment '学生学号', sname varchar(5) comment '学生姓名', sage datetime comment '学生出生日期', sgender char(1) comment '学生性别' )engine=innodb default charset=utf8; insert into student values (01,'赵雷','1990-01-01','男'); insert into student values (02,'钱电','1990-12-21','男'), (03,'孙风','1990-12-20','男'), (04,'李云','1990-12-06','男'), (05,'周梅','1991-12-01','女'), (06,'吴兰','1992-01-01','女'), (07,'郑竹','1989-01-01','女'), (08,'张三','2017-12-20','女'), (09,'李四','2017-12-25','女'), (10,'李四','2012-06-06','女'), (11,'赵六','2013-06-13','女'), (12,'孙七','2014-06-01','女'); -- 科目表 create table course ( cid int(10) comment '科目号', cname varchar(10) comment '科目名称', tid int(10) comment '授课老师' )engine=innodb default charset=utf8; insert into course values (01,'语文',02),(02,'数学',01),(03,'英语',03); --教师表 create table teacher( tid int(10) comment '教师学号', tname varchar(10) comment '教师姓名' )engine=innodb default charset=utf8; insert into teacher values(01,'张三'),(02,'李四'),(03,'王五'); --考试成绩表 create table score( sid int(10) comment '学生学号', cid int(10) comment '科目号', grade decimal(18,1) )engine=innodb default charset=utf8; insert into score values (01,01,80), (01,02,90), (01,03,99), (02,01,70), (02,02,60), (02,03,80), (03,01,90), (03,02,80), (03,03,80), (04,01,50), (04,02,30), (05,01,76), (05,02,87), (06,01,31), (06,03,34), (07,02,89), (07,03,98); -- 查询 01课程比02课程成绩高的学生的信息及课程分数 select s.sid,sname,sage,sgender,class1,class2 from student s inner join ( select a.sid,a.grade as class1,b.grade as class2 from score a inner join score b on a.sid=b.sid where a.cid=01 and b.cid=02 and a.grade>b.grade ) r on s.sid=r.sid; select s.sid,sname,sage,sgender,class1,class2 from student s inner join( select a.sid,class1,class2 from (select sid,grade as class1 from score where score.cid=01) a, (select sid,grade as class2 from score where score.cid=02) b where a.sid=b.sid and class1>class2 ) r on s.sid=r.sid; -- Q1.1:查询同时存在01和02课程的情况 select * from (select * from score where score.cid=01) a, (select * from score where score.cid=02) b where a.sid=b.sid; -- Q1.2:查询存在01课程但是可能不存在02课程的情况 select * from (select * from score where score.cid=01) a left join (select * from score where score.cid=02) b on a.sid=b.sid; -- 存在02但可能不存在01的情况 select * from (select * from score where score.cid=01) a right join (select * from score where score.cid=02) b on a.sid=b.sid; -- Q1.3:查询不存在01但存在02的课程的情况 select * from score where score.sid not in( select sid from score where score.cid=01 ) and score.cid=02; -- Q2:查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩 select s.sid,sname,avg_grade from student s inner join( select sid,avg(grade) as avg_grade from score group by sid having avg_grade>60 ) r on s.sid=r.sid; -- Q3:查询在 score 表存在成绩的学生信息 select s.sid,sname,sage,sgender from student s inner join ( select distinct sid from score ) r on s.sid=r.sid; -- Q4:查询所有同学的学生编号、学生姓名、选课总数、所有课程的成绩总和 select s.sid,sname,count_cid,sum_grade from student s left join ( select sid, count(cid) as count_cid, sum(grade) as sum_grade from score group by sid ) r on s.sid=r.sid; select s.sid,sname,count_cid,sum_grade from student s,( select sid, count(cid) as count_cid, sum(grade) as sum_grade from score group by sid ) r where s.sid=r.sid; -- Q4.1:查有成绩的学生信息 select s.sid,sname,sage,sgender from student s,( select distinct sid from score ) r where s.sid=r.sid; select * from student s where s.sid in(select distinct sid from score); -- Q5:查询 李 姓老师的数量 select count(*) from teacher where tname like '李%'; -- Q6:查询学过「张三」老师授课的同学的信息 select student.* from student,teacher,course,score where teacher.tname ='张三' and teacher.tid=course.tid and course.cid=score.cid and score.sid=student.sid; -- Q7:查询没有学全所有课程的同学的信息 select student.* from student where student.sid not in( select sid from score group by sid having count(cid)=(select count(cid) from course) ); -- Q8:查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 select * from student where sid in( select distinct sid from score where cid in( select cid from score where sid=01 ) ); -- Q9:查询和" 01 "号的同学学习的课程完全相同的其他同学的信息 select * from student where sid in( select sid from score where cid in( select cid from score where sid=01 ) group by sid ) and sid in( select sid from score group by sid having count(cid)=( select count(cid) from score where sid=01 ) ); -- Q10:查询没学过"张三"老师讲授的任一门课程的学生姓名 select * from student where sid not in( select sid from score where cid in( select cid from course where tid in( select tid from teacher where tname='张三' ) ) ); select * from student where sid not in( select sid from score,course,teacher where teacher.tname='张三' and teacher.tid=course.tid and score.cid=course.cid ); -- Q11:查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 select s.sid,sname,avg_grade from student s inner join ( select sid, avg(grade) as avg_grade from score where grade<60 group by sid having count(cid)>1 ) r on s.sid=r.sid -- Q12:检索" 01 "课程分数小于 60,按分数降序排列的学生信息 select s.sid,sname,sage,sgender,grade from student s inner join( select sid,grade from score where grade<60 and cid=01 order by grade desc ) r on s.sid=r.sid; select student.*,grade from student, score where score.cid=01 and score.grade<60 and student.sid=score.sid order by score.grade desc; -- Q13:按平均成绩从高到底显示所有学生的所有课程的成绩以及平均成绩 select score.sid,grade,avg_grade from score left join( select sid,avg(grade) as avg_grade from score group by sid ) r on score.sid=r.sid order by avg_grade desc; -- Q14:查询各科成绩最高分、最低分和平均分 select cid,max(grade) as max_grade, min(grade) as min_grade, avg(grade) as avg_grade from score group by cid; -- Q14.1:将各科的科目名称、选课人数也查询出来 select r.*,cname from course c inner join ( select cid,max(grade) as 最高分, min(grade) as 最低分, avg(grade) as 平均分, count(sid) as 选课人数 from score group by cid ) r on c.cid=r.cid; -- Q15:按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 set @rank=0; select cid,sid,grade,@rank:=@rank+1 as 排名 from ( select cid,sid,grade from score where cid=01 order by grade desc ) r; select a.cid, a.sid, a.grade, count(b.grade)+1 as rank from score as a left join score as b on a.grade<b.grade and a.cid = b.cid group by a.cid, a.sid,a.grade order by a.cid, rank ASC; -- Q16:查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 set @rank=0; select sid,总分,@rank:=@rank+1 as 排名 from ( select sid,sum(grade) as 总分 from score group by sid order by 总分 desc ) r; -- Q17:统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 select s.cid,cname, sum(case when grade<=100 and grade>85 then 1 else 0 end) as '[100-85]', sum(case when grade<=85 and grade>70 then 1 else 0 end) as '[85-70]', sum(case when grade<=70 and grade>60 then 1 else 0 end) as '[70-60]', sum(case when grade<=60 and grade>0 then 1 else 0 end) as '[60-0]' from score s left join course c on s.cid=c.cid group by s.cid; -- Q18:查询各科成绩前三名的记录 ? select * from ( select *,row_number() over(partition by cid order by grade desc) as rank from score ) r where rank<4; -- Q19:查询每门课程被选修的学生数 select cid,count(sid) from score group by cid; -- Q20:查询出只选修两门课程的学生学号和姓名 select sid,sname from student where sid in( select sid from score group by sid having count(cid)=2 ); -- Q21:查询男生、女生人数 select sgender,count(*) from student group by sgender; -- Q22:查询名字中含有 [风] 字的学生信息 select * from student where sname like '%风%'; -- Q23:查询同名学生名单,并统计同名人数 -- 统计同名人数 select sname,count(sname) as 同名人数 from student group by sname having count(sname)>1 -- 查询同名学生信息 select * from student s where sname in ( select sname from student group by sname having count(sname)>1 ); -- Q24:查询 1990 年出生的学生名单 select * from student where year(sage)='1990'; -- Q25:查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 select score.cid,cname,avg(grade) as 平均分 from course,score where course.cid=score.cid group by course.cid order by 平均分 desc,course.cid asc -- Q26:查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 select s.sid,sname,平均成绩 from student s inner join ( select sid,avg(grade) as 平均成绩 from score group by sid having 平均成绩>=85 ) r on s.sid=r.sid; select a.sid,sname,avg(grade) as 平均成绩 from student a, score b where a.sid=b.sid group by b.sid having 平均成绩>=85; -- Q27:查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 select sname,grade from student s inner join ( select sid,grade from score where cid in ( select cid from course where cname='数学' ) and grade<60 ) r on s.sid=r.sid; -- Q28:查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) select s.sid,sname,cid,grade from student s left join score c on s.sid=c.sid; -- Q29:查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 select sname,cname,grade from student a,course b,( select * from score where grade>70 group by sid,cid ) r where a.sid=r.sid and b.cid=r.cid select sname,cname,grade from student a,course b,score c where c.grade>70 and a.sid=c.sid and b.cid=c.cid -- Q30:查询存在不及格的课程 select cid from score where grade<60 group by cid -- Q31:查询课程编号为 01 且课程成绩在 80 分及以上的学生的学号和姓名 select a.sid,sname from student a,score b where b.cid=01 and grade>80 and a.sid=b.sid -- Q32:求每门课程的学生人数 select cid,count(*) as 学生人数 from score group by cid -- Q33:成绩不重复,查询选修【张三】老师所授课程的学生中,成绩最高的学生信息及其成绩 select student.sid,sname,grade from student,score where cid=( select cid from course where tid=( select tid from teacher where tname='张三' ) ) and student.sid=score.sid order by grade desc limit 1; -- Q34:成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 -- 先修改表,使得有两个最高分 update score set grade=90 where sid=07 and cid=02; select a.sid,sname,grade from student a,teacher b,course c,score d where b.tname='张三' and b.tid=c.tid and c.cid=d.cid and d.sid=a.sid and d.grade=( select max(grade) from student,teacher,course,score where teacher.tname='张三' and teacher.tid=course.tid and course.cid=score.cid and score.sid=student.sid ); -- Q35:查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 select a.sid,a.cid,a.grade from score a inner join score b on a.sid=b.sid and a.cid!=b.cid and a.grade=b.grade; -- Q36:查询每门课成绩最好的前两名 select a.sid,a.cid,a.grade from score as a left join score as b on a.cid = b.cid and a.grade<b.grade group by a.cid, a.sid having count(b.cid)<2 order by a.cid; -- Q37:统计每门课程的学生选修人数(超过 5 人的课程才统计) select cid,count(sid) from score group by cid having count(sid)>5; -- Q38:检索至少选修两门课程的学生学号 select sid from score group by sid having count(cid)>=2; -- Q39:查询选修了全部课程的学生信息 select * from student where sid in( select sid from score group by sid having count(cid)=( select count(cid) from course ) ); -- Q40:查询各学生的年龄,只按年份来算(一般都采用41中的计算方式) -- Q41 select sid,sname,timestampdiff(year,sage,curdate()) as 学生年纪 from student; -- Q42:查询本周过生日的学生 select * from student where weekofyear(sage)=weekofyear(curdate()); -- Q43:查询下周过生日的学生 select * from student where weekofyear(sage)=weekofyear(curdate())+1; -- Q44:查询本月过生日的学生 select * from student where month(sage)=month(curdate()); -- Q45:查询下月过生日的学生 select * from student where month(sage)=month(curdate())+1;
最新回复(0)