面試發現對查詢的理解還不夠,就繼續做了練習,這次對查詢有了更深的理解。
如果我的答題有問題,歡迎大家跟我交流。
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;