MySQL经典练习题及答案,常用SQL语句练习50题 您所在的位置:网站首页 mysql题目 MySQL经典练习题及答案,常用SQL语句练习50题

MySQL经典练习题及答案,常用SQL语句练习50题

2023-03-27 11:10| 来源: 网络整理| 查看: 265

表名和字段

–1.学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 –2.课程表 Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号 –3.教师表 Teacher(t_id,t_name) –教师编号,教师姓名 –4.成绩表 Score(s_id,c_id,s_score) –学生编号,课程编号,分数

测试数据 #–1.学生表 #Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 CREATE TABLE `Student` ( `s_id` VARCHAR(20), s_name VARCHAR(20) NOT NULL DEFAULT '', s_brith VARCHAR(20) NOT NULL DEFAULT '', s_sex VARCHAR(10) NOT NULL DEFAULT '', PRIMARY KEY(s_id) ); #–2.课程表 #Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号 create table Course( c_id varchar(20), c_name VARCHAR(20) not null DEFAULT '', t_id VARCHAR(20) NOT NULL, PRIMARY KEY(c_id) ); /* –3.教师表 Teacher(t_id,t_name) –教师编号,教师姓名 */ CREATE TABLE Teacher( t_id VARCHAR(20), t_name VARCHAR(20) NOT NULL DEFAULT '', PRIMARY KEY(t_id) ); /* –4.成绩表 Score(s_id,c_id,s_score) –学生编号,课程编号,分数 */ Create table Score( s_id VARCHAR(20), c_id VARCHAR(20) not null default '', s_score INT(3), primary key(`s_id`,`c_id`) ); 插入数据

#--插入学生表测试数据#('01' , '赵雷' , '1990-01-01' , '男')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('08' , '王菊' , '1990-01-20' , '女');

#--课程表测试数据insert into Course values('01' , '语文' , '02');insert into Course values('02' , '数学' , '01');insert into Course values('03' , '英语' , '03');

#--教师表测试数据insert into Teacher values('01' , '张三');insert into Teacher values('02' , '李四');insert into Teacher values('03' , '王五');

#--成绩表测试数据insert into Score values('01' , '01' , 80);insert into Score values('01' , '02' , 90);insert into Score values('01' , '03' , 99);insert into Score values('02' , '01' , 70);insert into Score values('02' , '02' , 60);insert into Score values('02' , '03' , 80);insert into Score values('03' , '01' , 80);insert into Score values('03' , '02' , 80);insert into Score values('03' , '03' , 80);insert into Score values('04' , '01' , 50);insert into Score values('04' , '02' , 30);insert into Score values('04' , '03' , 20);insert into Score values('05' , '01' , 76);insert into Score values('05' , '02' , 87);insert into Score values('06' , '01' , 31);insert into Score values('06' , '03' , 34);insert into Score values('07' , '02' , 89);insert into Score values('07' , '03' , 98);

练习题和sql语句 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数

-- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数select a.*,b.s_score as score01,c.s_score as score02 FROM   student a   JOIN score b ON a.s_id=b.s_id and b.c_id='01'   LEFT JOIN score c on a.s_id=c.s_id and c.c_id='02' or c.c_id = NULL WHERE b.s_score>c.s_score ;

2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数 -- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数 select a.* ,b.s_score as 01_score,c.s_score as 02_score from student a left join score b on a.s_id=b.s_id and b.c_id='01' or b.c_id=NULL join score c on a.s_id=c.s_id and c.c_id='02' where b.s_score=60; -- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩 -- (包括有成绩的和无成绩的) select b.s_id,b.s_name,ROUND(AVG(a.s_score),2) as avg_score from student b left join score a on b.s_id = a.s_id GROUP BY b.s_id,b.s_name HAVING ROUND(AVG(a.s_score),2)=60,中等为:70-80,优良为:80-90,优秀为:>=90 select a.c_id,b.c_name,MAX(s_score),MIN(s_score),ROUND(AVG(s_score),2), ROUND(100*(SUM(case when a.s_score>=60 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 及格率, ROUND(100*(SUM(case when a.s_score>=70 and a.s_score=80 and a.s_score=90 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 优秀率 from score a left join course b on a.c_id = b.c_id GROUP BY a.c_id,b.c_name -- 19、按各科成绩进行排序,并显示排名(实现不完全) -- mysql没有rank函数 select a.s_id,a.c_id, @i:=@i +1 as i保留排名, @k:=(case when @score=a.s_score then @k else @i end) as rank不保留排名, @score:=a.s_score as score from ( select s_id,c_id,s_score from score WHERE c_id='01' GROUP BY s_id,c_id,s_score ORDER BY s_score DESC )a,(select @k:=0,@i:=0,@score:=0)s union select a.s_id,a.c_id, @i:=@i +1 as i, @k:=(case when @score=a.s_score then @k else @i end) as rank, @score:=a.s_score as score from ( select s_id,c_id,s_score from score WHERE c_id='02' GROUP BY s_id,c_id,s_score ORDER BY s_score DESC )a,(select @k:=0,@i:=0,@score:=0)s union select a.s_id,a.c_id, @i:=@i +1 as i, @k:=(case when @score=a.s_score then @k else @i end) as rank, @score:=a.s_score as score from ( select s_id,c_id,s_score from score WHERE c_id='03' GROUP BY s_id,c_id,s_score ORDER BY s_score DESC )a,(select @k:=0,@i:=0,@score:=0)s -- 20、查询学生的总成绩并进行排名 select a.s_id, @i:=@i+1 as i, @k:=(case when @score=a.sum_score then @k else @i end) as rank, @score:=a.sum_score as score from (select s_id,SUM(s_score) as sum_score from score GROUP BY s_id ORDER BY sum_score DESC)a, (select @k:=0,@i:=0,@score:=0)s -- 21、查询不同老师所教不同课程平均分从高到低显示 select a.t_id,c.t_name,a.c_id,ROUND(avg(s_score),2) as avg_score from course a left join score b on a.c_id=b.c_id left join teacher c on a.t_id=c.t_id GROUP BY a.c_id,a.t_id,c.t_name ORDER BY avg_score DESC; -- 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩 select d.*,c.排名,c.s_score,c.c_id from ( select a.s_id,a.s_score,a.c_id,@i:=@i+1 as 排名 from score a,(select @i:=0)s where a.c_id='01' )c left join student d on c.s_id=d.s_id where 排名 BETWEEN 2 AND 3 UNION select d.*,c.排名,c.s_score,c.c_id from ( select a.s_id,a.s_score,a.c_id,@j:=@j+1 as 排名 from score a,(select @j:=0)s where a.c_id='02' )c left join student d on c.s_id=d.s_id where 排名 BETWEEN 2 AND 3 UNION select d.*,c.排名,c.s_score,c.c_id from ( select a.s_id,a.s_score,a.c_id,@k:=@k+1 as 排名 from score a,(select @k:=0)s where a.c_id='03' )c left join student d on c.s_id=d.s_id where 排名 BETWEEN 2 AND 3; -- 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比 select distinct f.c_name,a.c_id,b.`85-100`,b.百分比,c.`70-85`,c.百分比,d.`60-70`,d.百分比,e.`0-60`,e.百分比 from score a left join (select c_id,SUM(case when s_score >85 and s_score 85 and s_score 70 and s_score 70 and s_score 60 and s_score 60 and s_score =0 and s_score =0 and s_score =85 -- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数 select a.s_name,b.s_score from score b LEFT JOIN student a on a.s_id=b.s_id where b.c_id=( select c_id from course where c_name ='数学') and b.s_score=70 -- 37、查询不及格的课程 select a.s_id,a.c_id,b.c_name,a.s_score from score a left join course b on a.c_id = b.c_id where a.s_score80 -- 39、求每门课程的学生人数 select count(*) from score GROUP BY c_id; -- 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩 -- 查询老师id select c_id from course c,teacher d where c.t_id=d.t_id and d.t_name='张三' -- 查询最高分(可能有相同分数) select MAX(s_score) from score where c_id='02' -- 查询信息 select a.*,b.s_score,b.c_id,c.c_name from student a LEFT JOIN score b on a.s_id = b.s_id LEFT JOIN course c on b.c_id=c.c_id where b.c_id =(select c_id from course c,teacher d where c.t_id=d.t_id and d.t_name='张三') and b.s_score in (select MAX(s_score) from score where c_id='02') -- 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 select DISTINCT b.s_id,b.c_id,b.s_score from score a,score b where a.c_id != b.c_id and a.s_score = b.s_score -- 42、查询每门功成绩最好的前两名 -- 牛逼的写法 select a.s_id,a.c_id,a.s_score from score a where (select COUNT(1) from score b where b.c_id=a.c_id and b.s_score>=a.s_score)5 ORDER BY total,c_id ASC -- 44、检索至少选修两门课程的学生学号 select s_id,count(*) as sel from score GROUP BY s_id HAVING sel>=2 -- 45、查询选修了全部课程的学生信息 select * from student where s_id in( select s_id from score GROUP BY s_id HAVING count(*)=(select count(*) from course)) --46、查询各学生的年龄 -- 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 select s_birth,(DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(s_birth,'%Y') - (case when DATE_FORMAT(NOW(),'%m%d')>DATE_FORMAT(s_birth,'%m%d') then 0 else 1 end)) as age from student; -- 47、查询本周过生日的学生 select * from student where WEEK(DATE_FORMAT(NOW(),'%Y%m%d'))=WEEK(s_birth) select * from student where YEARWEEK(s_birth)=YEARWEEK(DATE_FORMAT(NOW(),'%Y%m%d')) select WEEK(DATE_FORMAT(NOW(),'%Y%m%d')) -- 48、查询下周过生日的学生 select * from student where WEEK(DATE_FORMAT(NOW(),'%Y%m%d'))+1 =WEEK(s_birth) -- 49、查询本月过生日的学生 select * from student where MONTH(DATE_FORMAT(NOW(),'%Y%m%d')) =MONTH(s_birth) -- 50、查询下月过生日的学生 select * from student where MONTH(DATE_FORMAT(NOW(),'%Y%m%d'))+1 =MONTH(s_birth)

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有