mysql 查询面试题 练习题 权亮

面试发现对查询的理解还不够,就继续做了练习,这次对查询有了更深的理解。
如果我的答题有问题,欢迎大家跟我交流。

create table student(
sid varchar(20),
sname varchar(20) not null default ‘’,
sbirth varchar(20) not null default ‘’,
ssex varchar (10) not null default ‘’,
primary key (sid)
);

create table course(
cid varchar(20),
cname varchar(20) not null default ‘’,
tid varchar(20) not null default ‘’,
primary key (cid)
);

create table teacher (
tid varchar (20),
tname varchar (20) not null default ‘’,
primary key (tid)
);

create table score (
sid varchar(20),
cid varchar(20) not null default ‘’,
sscore int(3),
primary key (sid,cid)
);

– 插入学生表测试数据
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);

select * FROM student;
select * from teacher;
select * from course;
select * from score;
use ceshi;

– 查询课程1成绩第二高的学生.
select * from score where cid=01 order by sscore desc limit 1,1;
select * from score where cid=01 order by sscore desc ;

select a.*, (select count( distinct sscore)+1
from score b where b.cid=a.cid and b.sscore>a.sscore) rank
from score a order by cid asc,sscore desc;

select a.*, (select count( distinct sscore)+1
from score b where b.cid=a.cid and b.sscore>a.sscore) rank
from score a where (select count( distinct sscore)+1
from score b where b.cid=a.cid and b.sscore>a.sscore)=3 order by cid asc,sscore desc ;

select cid,max(sscore) from score group by cid;
select group_concat(sid) ,max(sscore) from score group by cid;

select * from score a
where a.sscore >= ( select max(sscore) from score where cid=a.cid );
select max(sscore) from score group by cid;

select * from score a
where a.sscore = (select max(sscore) from score where cid=a.cid );

– 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数

select * from score;

select c.sname,a.sid,a.sscore course01,b.sscore course02 from student c, score a,score b where c.sid=a.sid and a.sid=b.sid and a.cid=01 and b.cid=02 and a.sscore>b.sscore;

select a.* ,b.sscore as 01_score,c.sscore as 02_score from
student a
join score b on a.sid=b.sid and b.cid=‘01’
left join score c on a.sid=c.sid and c.cid=‘02’ or c.cid = NULL where b.sscore>c.sscore;

select a.* ,b.,c. from student a join score b on a.sid=b.sid and b.cid=‘01’ left join score c on a.sid=c.sid and c.cid=‘02’ where b.sscore>c.sscore;

– 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数

select a.sname ,b.sscore 01score ,c.sscore 02score
from student a
join score b on a.sid=b.sid and b.cid=01
left join score c on b.sid=c.sid and c.cid=02
where b.sscore<c.sscore;

– 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

select b.sname,a.sid,avg(a.sscore) avgs
from score a
left join student b
on a.sid=b.sid
group by a.sid
having avgs>60 ;

– 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
– (包括有成绩的和无成绩的)
select b.sname,a.sid,avg(a.sscore) avgs
from score a
right join student b
on a.sid=b.sid
group by a.sid
having avgs<60 or avgs is null
order by avgs desc;

select b.sid,b.sname,ROUND(AVG(a.sscore),2) as avg_score from
student b
left join score a on b.sid = a.sid
GROUP BY b.sid,b.sname HAVING avg_score <60
union
select a.sid,a.sname,0 as avg_score from
student a
where a.sid not in (
select distinct sid from score);

– 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select a.sid,a.sname,count(b.cid) 选课总数, sum(sscore) 总成绩
from student a left join score b
on a.sid=b.sid
group by a.sid;

– 6、查询"李"姓老师的数量
select count(tname) from teacher where tname like ‘李%’;

– 7、查询学过"张三"老师授课的同学的信息

select a.*,d.tname from student a join score b on a.sid=b.sid
right join course c on b.cid=c.cid
right join teacher d on d.tid =c.tid where d.tname=‘张三’;

– 8、查询没学过"张三"老师授课的同学的信息
select * from score a ,

(select a.cid from course a left join teacher b on a.tid=b.tid where b.tname=‘张三’);

select * from score;

– 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

select * from score a ,score b where a.sid=b.sid and a.cid=01 and b.cid=02;

– 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
use ceshi;
select b.sid,b.sname from score a join student b on a.sid=b.sid where cid=01 and a.sid not in (select sid from score where cid=02);
select * from score;

– 11、查询没有学全所有课程的同学的信息

select b.sname,a.sid,count(cid) c from student b
left join score a on b.sid=a.sid
group by sid
having c<(select count(cid) from course);

– 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
select distinct(b.sid),b.sname,b.sbirth,b.ssex from student b join score a on a.sid=b.sid
where a.cid in (select cid from score where sid=01) and a.sid !=01;

select * from score;

– 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息

select a.sid from score a
group by a.sid having a.cid in
(select b.cid from score b where b.sid=01 ) and
count(a.cid) = (select count(c.cid) from score c where c.sid=01)
and a.sid!=01;

select * from score;
select b.cid from score b where b.sid=01;

select * from student a where a.sid in(
select b.sid from score b where b.cid in (
select cid from score where sid=01)
and sid!=01 group by sid having count(b.cid) =(select count(*) from score where sid=01));

use ceshi;

select sid from score where sid!=01 group by sid
having group_concat(cid order by cid) =
(select group_concat(cid order by cid) from score where sid=01);

select sid from student where sid!=01
and sid in (select sid from score where cid
in (select cid from score where sid=01)
group by sid having count(cid)
=(select count(cid) from score where sid=01));

select * from score;

– 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
use ceshi;

select sid,sname from student
where sid not in (select sid from score where cid =
(select cid from teacher a ,course b where a.tid=b.tid and a.tname=‘张三’));

select sid from student where sid not in
(select sid,count(cid) a from score group by sid
having cid not in (select cid from score where sid=01) and
sid=!‘01’ and a!=(select count(cid) from score where sid=01));

select sid from score where sid
not in (select sid from score where cid
not in (select cid from score where sid=01) )
group by sid
having count(cid)=(select count(cid) from score where sid=01) and sid!=01;

select sid from score where cid in
(select cid from score where sid =01)
group by sid having count(cid )=(select count(cid) from score where sid=01) and sid!=01;

– 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

– 16、检索"01"课程分数小于60,按分数降序排列的学生信息

– 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

– 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
select cid ,max(sscore),min(sscore),avg(sscore),count(sscore),
sum(case when sscore >=60 then 1 else 0 end )/ count(sscore) 及格率,
sum(case when sscore >=70 and sscore< 80 then 1 else 0 end )/ count(sscore) 中等率,
sum(case when sscore >=80 and sscore< 90 then 1 else 0 end )/ count(sscore) 优良率,
sum(case when sscore >=90 and sscore <=100 then 1 else 0 end )/ count(sscore) 优秀率
from score group by cid;

select * from score where cid=01;
select * from score where cid=02;
select * from score where cid=03;
select cid,count(sscore) from score group by cid;

– 19、按各科成绩进行排序,并显示排名
select sid ,cid,sscore,
(select count(sscore)+1 from score a where a.cid=b.cid and a.sscore>b.sscore) rank
from score b order by cid asc, sscore desc;

select sid ,cid,sscore,
(select count(distinct sscore)+1 from score a where a.cid=b.cid and a.sscore>b.sscore) rank
from score b order by cid asc, sscore desc;

– 20、查询学生的总成绩并进行排名

select sid ,sum(sscore) tot from score
group by sid order by tot desc;

– 21、查询不同老师所教不同课程平均分从高到低显示

select cid,avg(sscore) av from score
group by cid order by av desc;

– 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩

select sname, a.sid,cid,sscore ,
(select count(sscore)+1 from score c where c.cid=b.cid and c.sscore>b.sscore) rank
from student a ,score b
where a.sid=b.sid
and (select count(sscore)+1 from score c where c.cid=b.cid and c.sscore>b.sscore) <4
order by cid asc ,sscore desc ;

select a.,b., count(b.cid) rank from score a
left join score b on a.cid = b.cid and a.sscore<b.sscore order by a.cid asc,a.sscore desc;

select a.,b., count( distinct b.sscore)+1 rank from score a
left join score b on a.cid = b.cid and a.sscore<b.sscore
group by a.cid, a.sid order by a.cid asc,a.sscore desc;

select a.,b., count(distinct b.sscore)+1 rank from score a
left join score b on a.cid = b.cid and a.sscore<b.sscore
group by a.cid, a.sid having rank between 2 and 3 order by a.cid asc,a.sscore desc;

– 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
select count(a.sid), a.cid, cname,count(sscore),
sum(case when sscore>=0 and sscore <=60 then 1 else 0 end )/count(sscore) as '[0-60]所占百分比 ',
sum(case when sscore>60 and sscore <=70 then 1 else 0 end )/count(sscore) as ‘[70-60]所占百分比’ ,
sum(case when sscore>70 and sscore <=85 then 1 else 0 end )/count(sscore) as ‘[70-85]所占百分比’ ,
sum(case when sscore>85 and sscore <=100 then 1 else 0 end )/count(sscore) ‘[85-100]所占百分比’
from score a,course b
where a.cid=b.cid
group by cid;

select * from score order by cid;

– 24、查询学生平均成绩及其名次
select sid,avg(sscore) from score group by sid
order by avg(sscore) desc;

select sscore from score where sid=01;

– 25、查询各科成绩前三名的记录
– 1.选出b表比a表成绩大的所有组
– 2.选出比当前id成绩大的 小于三个的

– 26、查询每门课程被选修的学生数
select count(sid) from score
group by cid;

– 27、查询出只有两门课程的全部学生的学号和姓名

select a.sid,sname,count(cid) from score a ,student b where a.sid=b.sid
group by sid having count(cid )=2 ;

– 28、查询男生、女生人数

select
count(ssex), ssex from student group by ssex;
select * from student;

– 29、查询名字中含有"风"字的学生信息

select * from student where sname like ‘%风%’;

– 30、查询同名同性学生名单,并统计同名人数
use ceshi;
select * from student;
select a.ssex,a.sname,b.* from student a,student b where a.ssex=b.ssex and a.sname=b.sname and a.sid!=b.sid;

select a.sname,a.ssex,count(*) from student a  JOIN 
				student b on a.sid !=b.sid and a.sname = b.sname and a.ssex = b.ssex
	GROUP BY a.sname,a.ssex;

– 31、查询1990年出生的学生名单
select * from student where year(sbirth)=1990;

– 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列

select cid, avg(sscore) a from score group by cid order by a desc,cid asc;

– 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
select a.sid,b.sname,avg(a.sscore) c from score a ,student b where a.sid=b.sid group by b.sid having c>=85;

– 34、查询课程名称为"数学",且分数低于60的学生姓名和分数

select a.sname,a.sid,b.sscore
from student a ,score b,course c
where a.sid=b.sid and b.cid=c.cid and c.cname=‘数学’ and b.sscore<60;

select * from course;

– 35、查询所有学生的课程及分数情况;
select a.sid,c.sname,
sum(case when b.cname=‘语文’ then a.sscore else 0 end ) 语文,
sum(case when b.cname=‘数学’ then a.sscore else 0 end) 数学,
sum(case when b.cname=‘英语’ then a.sscore else 0 end ) 英语
from score a,course b,student c
where a.cid=b.cid and a.sid=c.sid
group by a.sid;

		select a.sid,c.sname,
(case when b.cname='语文' then a.sscore else 0 end ) 语文,
(case when b.cname='数学' then a.sscore else 0 end) 数学,
(case when b.cname='英语' then a.sscore else 0 end ) 英语
from score a,course b,student c 
where a.cid=b.cid and a.sid=c.sid;

select * from score;

– 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
select sname,a.sid,group_concat(cname) from student a, score b,course c
where a.sid=b.sid and b.cid=c.cid
and a.sid not in ( select sid from score where sscore<70 )
group by sid;

select * from score;

– 37、查询不及格的课程
select cid from score where sscore<60 group by cid;

– 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;

select a.sid,a.sname,b.sscore
from student a ,score b
where a.sid=b.sid
and b.cid=01 and b.sscore>80;
select * from score where cid =01;

– 39、求每门课程的学生人数

select cid, count(sid) from score group by cid;
use ceshi;

– 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
select a.*,max(b.sscore)
from student a , score b ,course c ,teacher d
where a.sid=b.sid and b.cid=c.cid and c.tid=d.tid and tname=‘张三’ group by b.cid;

– 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
select * from score a ,score b where a.sscore=b.sscore and a.cid!=b.cid;

– 42、查询每门功成绩最好的前两名
select sid,cid,sscore,(select count(sscore)+1 from score a where a.cid=b.cid and a.sscore>b.sscore) rank
from score b
where (select count(sscore)+1 from score a where a.cid=b.cid and a.sscore>b.sscore)❤️
order by cid asc,sscore desc;

– 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select cid ,if(count()>5,count(),null) rank
from score
group by cid
order by rank desc,cid asc;

– 44、检索至少选修两门课程的学生学号

select sid from student
where sid
in (select sid from score group by sid
having count(cid)>=2);

select * from score;

– 45、查询选修了全部课程的学生信息

select sid from student
where sid in
(select sid from score group by sid having count(cid)=(select count(*) from course));

– 46、查询各学生的年龄
– 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一

select year(from_days( datediff(curdate(),sbirth))) from student;
select * from student;
select sid,timestampdiff(year,sbirth,curdate()) from student;

– 47、查询本周过生日的学生
select 7-(dayofweek(curdate())-1);
select date_format( sbirth) from student;
use ceshi;
select * from student where week(date_format(sbirth,’%y-=%m-%d’))=week(curdate())+1;

  select * from student where week(sbirth)=week(curdate()); 

select curdate();
select week(sbirth) from student;

– 48、查询下周过生日的学生

select * from student where week(sbirth)=week(curdate())+1;
– 49、查询本月过生日的学生
select * from student where month(sbirth)=month(curdate());

– 50、查询下月过生日的学生
select * from student where month(sbirth)=month(curdate())+1;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章