一、常見的SQL面試題:經典50題。
來自於知乎分享https://zhuanlan.zhihu.com/p/38354000(部分題目)
表結構:
學生表:student(學號,學生姓名,出生年月,性別)
成績表:score(學號,課程號,成績)
課程表:course(課程號,課程名稱,教師號)
教師表:teacher(教師號,教師姓名)
1. 查詢各科成績最高和最低的分, 以如下的形式顯示:課程號,最高分,最低分(groupby分組聚合)
select courseid,max(score),min(score) from score group by courseid;
2. 查詢平均成績大於60分學生的學號和平均成績(having)
select stuid,avg(score) from score group by stuid having avg(score) > 60;
3. 查詢同名同性學生名單並統計同名人數
select stuname,count(stuid) from student group by stuname having count(stuid) > 1;
4. 查詢每門課程的平均成績,結果按平均成績升序排序,平均成績相同時,按課程號降序排列
select courseid,avg(score) from score group by courseid order by avg(score),courseid desc;
6. 查詢兩門以上不及格課程的同學的學號及其平均成績
select stuid,avg(score) as avg_score from score where score < 60 group by stuid having count(courseid) >=2;
7. 查詢所有課程成績小於80分學生的學號、姓名
select student.stuid,student.stuname from student where stuid not in( select distinct stuid from score where score >80 );
8. 1990年出生的學生名單(stubirtthdate為date類型)
select stuid,stuname from student where year(stubirthdate) = 1990;
9.
TOPN問題 查詢各科成績最高的記錄(關聯子查詢)
select * from score as a where score = (select max(score) from score as b where a.courseid = b.courseid);
上述關聯子查詢順序
1. select * from score 2. select max(score) from score where courseid = 0001 3. select * from score where score =81 and courseid = 0001
TOPN問題 查詢各科成績前兩名的記錄(union)
(select * from score where courseid = 0001 order by score desc limit 2) union (select * from score where courseid = 0002 order by score desc limit 2) union (select * from score where courseid = 0003 order by score desc limit 2);
10. 查詢出每門課程的及格人數和不及格人數(case表達式)
select courseid, sum(case when score >= 60 then 1 else 0 end) as jigerenshu, sum(case when score < 60 then 1 else 0 end) as bujigerenshu from score group by courseid;
11. 使用分段[100-85],[85-70],[70-60],[<60]來統計各科成績,分別統計:各分數段人數,課程號和課程名稱(case表達式)
select score.courseid,course.coursename, sum(case when score between 85 and 100 then 1 else 0 end) as '[85-100]', sum(case when score between 70 and 85 then 1 else 0 end) as '[70-85]', sum(case when score between 60 and 70 then 1 else 0 end) as '[60-70]' from score right join course on score.courseid = course.courseid group by courseid;
12. 行列互換
參考https://mp.weixin.qq.com/s/6Kll4Q6Xp37i2PiLUh4cMA
下面是學生的成績表(表名score,列名:學號、課程號、成績)
使用sql實現將該表行轉列為下面的表結構
select stuid, max(case courseid when '0001' then score else 0 end) as '課程號0001', max(case courseid when '0002' then score else 0 end) as '課程號0002', max(case courseid when '0003' then score else 0 end) as '課程號0003' from score group by stuid;
二、互聯網校招SQL筆試經典50題及答案解析
來自知乎分享https://zhuanlan.zhihu.com/p/53302593(部分題目)
表結構
學生表Student
教師表Teacher
課程表Course
成績表SC
1. 查詢“01”課程比“02”課程成績高的所有學生的學號(生成新表 表連接)
select distinct t1.sid as sid from (select * from sc where cid='01') as t1 left join (select * from sc where cid='02') as t2 on t1.sid=t2.sid where t1.score>t2.score;
2. 查詢學過編號“01”並且也學過編號“02”課程的同學的學號、姓名
select sid,sname from student where sid IN (select `sid` from SC group by `sid` #分組保證 學生 01 和 02課程都學過 having count(if(cid='01',score,null))>0 and count(if(cid='02',score,null))>0);
3. 查詢所有課程成績小於60分的同學的學號、姓名;
select s_sid.`sid`,student.sname from (select distinct `sid` from sc group by `sid` having max(score) < 60)s_sid left join student on s_sid.`sid` = student.`sid`;
4. 查詢沒有學全所有課的同學的學號、姓名;
select ssid.`sid`,student.sname from (select `sid` from sc group by `sid` having count(`score`) < (select count(*) from course))ssid left join student on ssid.`sid` = student.`sid` ;
5. 查詢至少有一門課與學號為“01”的同學所學相同的同學的學號和姓名
select distinct sc.sid from ( select cid from sc where sid='01' )t1 left join sc on t1.cid=sc.cid;
6. 查詢和"01"號的同學學習的課程完全相同的其他同學的學號和姓名
select t1.`sid` ,student.sname from (select sc.`sid` from ( select cid from sc where `sid` = 01 )t1 left join sc on t1.`cid` = sc.`cid` group by `sid` having count(distinct sc.cid)= (select count(distinct cid) from sc where sid = '01'))t1 left join student on t1.`sid` = student.`sid`;
7. 查詢兩門及其以上不及格課程的同學的學號,姓名及其平均成績
select ssid.`sid`,student.sname,ssid.`avg(score)` from ( select sid,avg(score) from sc group by sid having count(if(score<60,score,NULL)) >= 2 )ssid left join student on ssid.`sid` = student.`sid`;
8. 查詢所有課程的成績第2名到第3名的學生信息及該課程成績(窗口函數)
select `sid`,rank_num,score,`cid` from ( select rank() over(PARTITION by `cid` order by score desc) as rank_num, `sid`,`score`,`cid` from sc )t where rank_num in (2,3);
9. 統計各科成績各分數段人數:課程編號,課程名稱,[100-85],[85-70],[70-60],[0-60]及所占百分比
select sc.`cid`,cname, count(if(score between 85 and 100,sid,NULL))/count(sid) ,count(if(score between 70 and 85,sid,null))/count(sid) ,count(if(score between 60 and 70,sid,null))/count(sid) ,count(if(score between 0 and 60,sid,null))/count(sid) from sc left join course on sc.`cid` = course.`cid` group by sc.`cid`;
10. 查詢學生平均成績及其名次(窗口函數)
select sid,avg_score,rank() over(order by avg_score desc) from (select sid,avg(score) as avg_score from sc group by `sid`)t;
11. 查詢各科成績前三名的記錄(窗口函數)
select * from (select `cid`,`sid`,rank() over (PARTITION by `cid` order by score desc) as rank_num from sc)t where rank_num <=3;
12. 查詢各學生的年齡 (curdate())
select sid,sname,year(curdate())-year(sage) as age from student;
2020.4.19 15:00