SQL面試經(jīng)典50題

SQL語句的執(zhí)行順序:



創(chuàng)建school數(shù)據(jù)庫

create database school;
use school;

創(chuàng)建四張表

create table student(
    s_id varchar(10),
    s_name varchar(20),
    s_age date,
    s_sex varchar(10)
);

create table course(
    c_id varchar(10),
    c_name varchar(20),
    t_id varchar(10)
);


create table teacher (
t_id varchar(10),
t_name varchar(20)
);

create table score (
    s_id varchar(10),
    c_id varchar(10),
    score varchar(10)
);

往表里插值

insert into student (s_id, s_name, s_age, s_sex)
values  ('01' , '趙雷' , '1990-01-01' , '男'),
        ('02' , '錢電' , '1990-12-21' , '男'),
        ('03' , '孫風(fēng)' , '1990-05-20' , '男'),
        ('04' , '李云' , '1990-08-06' , '男'),
        ('05' , '周梅' , '1991-12-01' , '女'),
        ('06' , '吳蘭' , '1992-03-01' , '女'),
        ('07' , '鄭竹' , '1989-07-01' , '女'),
        ('08' , '王菊' , '1990-01-20' , '女');

insert into course (c_id, c_name, t_id)
values  ('01' , '語文' , '02'),
        ('02' , '數(shù)學(xué)' , '01'),
        ('03' , '英語' , '03');

insert into teacher (t_id, t_name)
values  ('01' , '張三'),
        ('02' , '李四'),
        ('03' , '王五');

insert into score (s_id, c_id, score)
values  ('01' , '01' , 80),
        ('01' , '02' , 90),
        ('01' , '03' , 99),
        ('02' , '01' , 70),
        ('02' , '02' , 60),
        ('02' , '03' , 80),
        ('03' , '01' , 80),
        ('03' , '02' , 80),
        ('03' , '03' , 80),
        ('04' , '01' , 50),
        ('04' , '02' , 30),
        ('04' , '03' , 20),
        ('05' , '01' , 76),
        ('05' , '02' , 87),
        ('06' , '01' , 31),
        ('06' , '03' , 34),
        ('07' , '02' , 89),
        ('07' , '03' , 98);

看下建好的四張表


四張表

創(chuàng)建一張總總表

create table total(
select a.s_id as s_id,a.s_name as s_name,a.s_age as s_age,a.s_sex as s_sex,
b.c_id as c_id,b.score as score,c.t_id as t_id,d.t_name as t_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
left join
teacher d on c.t_id=d.t_id
);
select * from total;
學(xué)生課程成績查詢總表total

***1纳本、查詢"01"課程比"02"課程成績高的學(xué)生的信息及課程分?jǐn)?shù)

select a.s_id as s_id,score1,score2 from
(select s_id, score as score1 from score where c_id='01') a
inner join
(select s_id, score as score2 from score where c_id='02') b
on a.s_id=b.s_id
where score1>score2;

2椎扬、查詢"01"課程比"02"課程成績低的學(xué)生的信息及課程分?jǐn)?shù)

select a.s_id as s_id,score1,score2 from
(select s_id, score as score1 from score where c_id='01') a
inner join
(select s_id, score as score2 from score where c_id='02') b
on a.s_id=b.s_id
where score1<score2;

3、查詢平均成績大于等于60分的同學(xué)的學(xué)生編號和學(xué)生姓名和平均成績

select student.s_id as s_id,student.s_name as s_name,b.avg_score as avg_score from student 
right join 
(select s_id,avg(score) as avg_score from score
group by s_id having avg_score>60) b
on student.s_id=b.s_id;

4涨冀、查詢平均成績小于60分的同學(xué)的學(xué)生編號和學(xué)生姓名和平均成績

select student.s_id as s_id,student.s_name as s_name,b.avg_score as avg_score from student 
right join 
(select s_id,avg(score) as avg_score from score
group by s_id having avg_score<60) b
on student.s_id=b.s_id;

5梨睁、查詢所有同學(xué)的學(xué)生編號、學(xué)生姓名、選課總數(shù)棵帽、所有課程的總成績

select s_id, s_name, count(c_id) as c_num, sum(score) as total_score
from total
group by s_id ;

6、查詢"李"姓老師的數(shù)量

select count(t_name) from teacher
where t_name like '李%';

7渣玲、查詢學(xué)過"張三"老師授課的同學(xué)的信息

select distinct s_id,s_name,s_age,s_sex
from total
where t_name='張三';

8逗概、查詢沒學(xué)過"張三"老師授課的同學(xué)的信息

select * from student
where s_id not in
(select distinct s_id
from total
where t_name='張三');

9、查詢學(xué)過編號為"01"并且也學(xué)過編號為"02"的課程的同學(xué)的信息

select * from student
where s_id in
(select s_id from score where c_id='01')
and s_id in
(select s_id from score where c_id='02');

10忘衍、查詢學(xué)過編號為"01"但是沒有學(xué)過編號為"02"的課程的同學(xué)的信息

select * from student
where s_id in
(select s_id from score where c_id='01')
and s_id not in
(select s_id from score where c_id='02');

11逾苫、查詢沒有學(xué)全所有課程的同學(xué)的信息

select s_id, s_name, s_age, s_sex from total
group by s_id having count(c_id) <3 ;

12、查詢至少有一門課與學(xué)號為"01"的同學(xué)所學(xué)相同的同學(xué)的信息

思路:先找出‘01’同學(xué)學(xué)過的c_id枚钓,再找出學(xué)過任一門的s_id铅搓,再根據(jù)s_id在student找學(xué)生信息。

select * from student
where s_id in
(select distinct s_id from score
where c_id in
(select c_id from score where s_id='01'));

***13搀捷、查詢和"01"號的同學(xué)學(xué)習(xí)的課程完全相同的其他同學(xué)的信息

思路:先找學(xué)過‘01’同學(xué)學(xué)過的課程的學(xué)生星掰,然后通過group by找這些人里面學(xué)的課程數(shù)和‘01’相同的人。比如下面嫩舟,表a是‘01’同學(xué)學(xué)過的課程氢烘,b則是所有學(xué)過‘01’同學(xué)學(xué)過的任一門課程的人。

select * from student
where s_id in
(select s_id from
(select score.s_id,a.c_id from 
(select c_id from score where s_id='01') a 
inner join  score
on a.c_id=score.c_id) b
where s_id<>'01' 
group by s_id having count(c_id)=
(select count(c_id) from score where s_id='01'));

14家厌、查詢沒學(xué)過"張三"老師講授的任一門課程的學(xué)生姓名

select s_id,s_name from student
where s_id not in
(select distinct s_id from total
where t_name='張三');

15播玖、查詢兩門及其以上不及格課程的同學(xué)的學(xué)號,姓名及其平均成績

思路:先找不及格超過兩門的s_id饭于,為表a蜀踏,再根據(jù)表a連接學(xué)生信息表student和平均分表b维蒙。

select a.s_id,student.s_name,b.avg_score from
(select s_id from score
where score<60
group by s_id having count(*)>=2) a
left join
student on a.s_id=student.s_id
left join
(select s_id,avg(score) as avg_score
from score
group by s_id) b
on a.s_id=b.s_id;

16、檢索"01"課程分?jǐn)?shù)小于60脓斩,按分?jǐn)?shù)降序排列的學(xué)生信息

select a.s_id,student.s_name,student.s_age,student.s_sex,a.score from
(select s_id,score from score
where c_id='01' and score<60
order by score desc) a
left join student on a.s_id=student.s_id;

***17木西、按平均成績從高到低顯示所有學(xué)生的所有課程的成績以及平均成績

select s_id as '學(xué)號',
sum(case c_id when '01' then score else 0 end) as '語文',
sum(case c_id when '02' then score else 0 end) as '數(shù)學(xué)',
sum(case c_id when '03' then score else 0 end) as '英語',
avg(score) as '平均成績'
from score
group by s_id
order by '平均成績' desc;

*****18、查詢各科成績最高分随静、最低分和平均分:以如下形式顯示:課程ID八千,課程name,最高分燎猛,最低分恋捆,平均分,及格率重绷,中等率沸停,優(yōu)良率,優(yōu)秀率

----及格為>=60昭卓,中等為:70-80愤钾,優(yōu)良為:80-90,優(yōu)秀為:>=90

select a.c_id as '課程ID',course.c_name as '課程name',
max(a.score) as '最高分',min(a.score) as '最低分',
cast(avg(a.score) as decimal(5,2)) as '平均分',
concat(cast(sum(pass)/count(*)*100 as decimal(5,2)),'%') as '及格率',
concat(cast(sum(medi)/count(*)*100 as decimal(5,2)),'%') as '中等率',
concat(cast(sum(good)/count(*)*100 as decimal(5,2)),'%') as '優(yōu)良率',
concat(cast(sum(excellent)/count(*)*100 as decimal(5,2)),'%') as '優(yōu)秀率' from
(select * ,
case when score>=60 then 1 else 0 end as pass,
case when score>=70 and score<80 then 1 else 0 end as medi,
case when score>=80 and score<90 then 1 else 0 end as good,
case when score>=90 then 1 else 0 end as excellent
from score) a
left join course on a.c_id=course.c_id
group by a.c_id;

19候醒、按各科成績進(jìn)行排序能颁,并顯示排名

select a.*,@rank:=@rank+1 as rank from
(select c_id,sum(score) as '成績' from score
group by c_id order by sum(score) desc) a,
(select @rank:=0) b;

20、查詢學(xué)生的總成績并進(jìn)行排名

select a.*,@rank:=@rank+1 as rank from 
(select s_id,sum(score) as '總成績' from score
group by s_id order by sum(score) desc) a,
(select @rank:=0) b;

21倒淫、查詢不同老師所教不同課程平均分從高到低顯示

select t_id,t_name,c_id,avg(score) as avg_score 
from total
group by t_id,c_id
order by avg_score desc;

****22伙菊、查詢所有課程的成績第2名到第3名的學(xué)生信息及該課程成績

先得到一張每門課程的成績排序表

select c_id,s_id,score from score 
group by c_id,s_id order by c_id,score desc;

添加兩個輔助變量用來生成分組排名

select *,if(@pa=a.c_id,@rank:=@rank+1,@rank:=1) AS rank,@pa:=a.c_id
from
(select c_id,s_id,score from score 
group by c_id,s_id order by c_id,score desc) a,
(select @rank:=0,@pa:=NULL) b;

選出排名為2-3名與student表連接查詢

select result.c_id,result.s_id,result.score,
student.s_name,student.s_age,student.s_sex from
(select *,if(@pa=a.c_id,@rank:=@rank+1,@rank:=1) AS rank,@pa:=a.c_id
from
(select c_id,s_id,score from score 
group by c_id,s_id order by c_id,score desc) a,
(select @rank:=0,@pa:=NULL) b) result
left join student on result.s_id=student.s_id
where rank between 2 and 3
group by c_id,score desc;

這樣寫其實(shí)也是有問題的,就是沒有考慮分?jǐn)?shù)相同的人

23敌土、統(tǒng)計各科成績各分?jǐn)?shù)段人數(shù):課程編號,課程名稱,[100-85],[85-70],[70-60],[0-60]及所占百分比

select a.c_id as '課程編號',course.c_name as '課程名稱',
sum(level1) as '[100-85]人數(shù)', sum(level1)/count(1) as '[100-85]占比',
sum(level2) as '[85-70]人數(shù)', sum(level2)/count(1) as '[85-70]占比',
sum(level3) as '[70-60]人數(shù)', sum(level3)/count(1) as '[70-60]占比',
sum(level4) as '[0-60]人數(shù)', sum(level4)/count(1) as '[0-60]占比' from
(select *,
(case when score between 85 and 100 then 1 else 0 end) as 'level1',
(case when score between 70 and 84 then 1 else 0 end) as 'level2',
(case when score between 60 and 69 then 1 else 0 end) as 'level3',
(case when score between 0 and 59 then 1 else 0 end) as 'level4'
from score) a
left join course on a.c_id=course.c_id
group by a.c_id;

24镜硕、查詢學(xué)生平均成績及其名次

select a.*,@rank:=@rank+1 as rank from 
(select s_id,avg(score) as '平均成績' from score
group by s_id order by avg(score) desc) a,
(select @rank:=0) b;

****25、查詢各科成績前三名的記錄

select a.c_id,a.s_id,a.score 
from score a 
where (select count(b.s_id) from score b where a.c_id=b.c_id and a.score<b.score)<3 
group by a.c_id,a.s_id; 

26返干、查詢每門課程被選修的學(xué)生數(shù)

select c_id,count(s_id) as '選修人數(shù)' 
from score group by c_id; 

27兴枯、查詢出只有兩門課程的全部學(xué)生的學(xué)號和姓名

select student.* from
(select s_id from score
group by s_id having count(c_id)=2) a
left join student on a.s_id=student.s_id;

28、查詢男生矩欠、女生人數(shù)

select s_sex as '性別',count(1) as '人數(shù)'
from student group by s_sex;

29财剖、查詢名字中含有"風(fēng)"字的學(xué)生信息

select * from student
where s_name like '%風(fēng)%';

30、查詢同名同姓學(xué)生名單晚顷,并統(tǒng)計同名人數(shù)

select s_name,num as '同名人數(shù)' from 
(select *,count(s_id)-1 as num 
from student group by s_name) a;

31峰伙、查詢1990年出生的學(xué)生名單(注:Student表中Sage列的類型是datetime)

select s_name from student where year(s_age)='1990';

32、查詢每門課程的平均成績该默,結(jié)果按平均成績降序排列瞳氓,平均成績相同時,按課程編號

select c_id,avg(score) as '平均成績'
from score group by c_id
order by 平均成績 desc,c_id;

33、查詢平均成績大于等于85的所有學(xué)生的學(xué)號匣摘、姓名和平均成績

select a.s_id,s_name,avg_score from 
(select s_id,avg(score) as avg_score from score
group by s_id having avg(score)>=85) a
left join student on a.s_id=student.s_id;
image.png

34店诗、查詢課程名稱為"數(shù)學(xué)",且分?jǐn)?shù)低于60的學(xué)生姓名和分?jǐn)?shù)

select s_name,c_name,score from total
where c_name='數(shù)學(xué)' and score<60;

35音榜、查詢所有學(xué)生的課程及分?jǐn)?shù)情況

select s_id,
sum(case when c_id='01' then score else 0 end) as '語文',
sum(case when c_id='02' then score else 0 end) as '數(shù)學(xué)',
sum(case when c_id='03' then score else 0 end) as '英語'
from total
group by s_id;

36庞瘸、查詢?nèi)魏我婚T課程成績在70分以上的姓名、課程名稱和分?jǐn)?shù)

select s_name,c_name,score
from total where score>70;

37赠叼、查詢不及格的課程

select score.c_id,course.c_name,score
from score left join course
on score.c_id=course.c_id
where score<60;

38擦囊、查詢課程編號為01且課程成績在80分以上的學(xué)生的學(xué)號和姓名

select student.s_id,s_name from student
right join score on student.s_id=score.s_id
where c_id='01' and score>80;

因?yàn)椤?1’課程最高分為80,所以查詢結(jié)果為空嘴办。

39瞬场、求每門課程的學(xué)生人數(shù)

select c_id,count(1) as '選課人數(shù)'
from score group by c_id;

40、查詢選修"張三"老師所授課程的學(xué)生中涧郊,成績最高的學(xué)生信息及其成績

select student.*,a.score from
(select s_id,score
from total where t_name='張三'
order by score desc limit 1) a
left join student on a.s_id=student.s_id;

41贯被、查詢不同課程成績相同的學(xué)生的學(xué)生編號、課程編號妆艘、學(xué)生成績

select a.s_id,a.c_id,a.score
from score a,score b
where a.c_id=b.c_id and a.s_id<>b.s_id and a.score=b.score;

42彤灶、查詢每門功成績最好的前兩名

(select c_id,s_id from score where c_id='01' order by score limit 2)
union
(select c_id,s_id from score where c_id='02' order by score limit 2)
union
(select c_id,s_id from score where c_id='03' order by score limit 2);

43、統(tǒng)計每門課程的學(xué)生選修人數(shù)(超過5人的課程才統(tǒng)計)批旺。要求輸出課程號和選修人數(shù)幌陕,查詢結(jié)果按人數(shù)降序排列,若人數(shù)相同朱沃,按課程號升序排列

select c_id,count(s_id) as 選修人數(shù) from score
group by c_id having 選修人數(shù)>5
order by 選修人數(shù) desc,c_id;

44苞轿、檢索至少選修兩門課程的學(xué)生學(xué)號

select s_id from score group by s_id having count(c_id)>=2;

45茅诱、查詢選修了全部課程的學(xué)生信息

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

46逗物、查詢各學(xué)生的年齡

select s_id,s_name,(year(now())-year(s_age)) as '年齡' from student;

***47、查詢本周過生日的學(xué)生

思路:找到這周的起始日期(一周的開始從周日算起)

select s_name,s_age from student
where date_format(s_age,'2019-%m-%d') 
between adddate(curdate(),-(date_format(now(),'%w')))
and adddate(curdate(),7-date_format(now(),'%w'));

因?yàn)闆]有人這周過生日瑟俭,因此查詢記錄為空

48翎卓、查詢下周過生日的學(xué)生

select s_name,s_age from student
where date_format(s_age,'2019-%m-%d') 
between adddate(curdate(),7-(date_format(now(),'%w')))
and adddate(curdate(),14-date_format(now(),'%w'));

沒人下周過生日,查詢記錄同樣為空

49摆寄、查詢本月過生日的學(xué)生

select s_name,s_age from student
where date_format(s_age,'%m')=date_format(now(),'%m');

(小白寫這篇的時候是7月)

50失暴、查詢下月過生日的學(xué)生

select s_name,s_age from student
where date_format(s_age,'%m')=date_format(now(),'%m')+1;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市微饥,隨后出現(xiàn)的幾起案子逗扒,更是在濱河造成了極大的恐慌,老刑警劉巖欠橘,帶你破解...
    沈念sama閱讀 221,576評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件矩肩,死亡現(xiàn)場離奇詭異,居然都是意外死亡肃续,警方通過查閱死者的電腦和手機(jī)黍檩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,515評論 3 399
  • 文/潘曉璐 我一進(jìn)店門叉袍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人刽酱,你說我怎么就攤上這事喳逛。” “怎么了棵里?”我有些...
    開封第一講書人閱讀 168,017評論 0 360
  • 文/不壞的土叔 我叫張陵润文,是天一觀的道長。 經(jīng)常有香客問我殿怜,道長转唉,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,626評論 1 296
  • 正文 為了忘掉前任稳捆,我火速辦了婚禮赠法,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘乔夯。我一直安慰自己砖织,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,625評論 6 397
  • 文/花漫 我一把揭開白布末荐。 她就那樣靜靜地躺著侧纯,像睡著了一般。 火紅的嫁衣襯著肌膚如雪甲脏。 梳的紋絲不亂的頭發(fā)上眶熬,一...
    開封第一講書人閱讀 52,255評論 1 308
  • 那天,我揣著相機(jī)與錄音块请,去河邊找鬼娜氏。 笑死,一個胖子當(dāng)著我的面吹牛墩新,可吹牛的內(nèi)容都是我干的贸弥。 我是一名探鬼主播,決...
    沈念sama閱讀 40,825評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼海渊,長吁一口氣:“原來是場噩夢啊……” “哼绵疲!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起臣疑,我...
    開封第一講書人閱讀 39,729評論 0 276
  • 序言:老撾萬榮一對情侶失蹤盔憨,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后讯沈,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體郁岩,經(jīng)...
    沈念sama閱讀 46,271評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,363評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了驯用。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片脸秽。...
    茶點(diǎn)故事閱讀 40,498評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖蝴乔,靈堂內(nèi)的尸體忽然破棺而出记餐,到底是詐尸還是另有隱情,我是刑警寧澤薇正,帶...
    沈念sama閱讀 36,183評論 5 350
  • 正文 年R本政府宣布片酝,位于F島的核電站,受9級特大地震影響挖腰,放射性物質(zhì)發(fā)生泄漏雕沿。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,867評論 3 333
  • 文/蒙蒙 一猴仑、第九天 我趴在偏房一處隱蔽的房頂上張望审轮。 院中可真熱鬧,春花似錦辽俗、人聲如沸疾渣。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,338評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽榴捡。三九已至,卻和暖如春朱浴,著一層夾襖步出監(jiān)牢的瞬間吊圾,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,458評論 1 272
  • 我被黑心中介騙來泰國打工翰蠢, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留项乒,地道東北人。 一個月前我還...
    沈念sama閱讀 48,906評論 3 376
  • 正文 我出身青樓躏筏,卻偏偏與公主長得像板丽,于是被迫代替她去往敵國和親呈枉。 傳聞我的和親對象是個殘疾皇子趁尼,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,507評論 2 359

推薦閱讀更多精彩內(nèi)容