经典mysql题_MySQL经典题目

一、常见的SQL面试题:经典50题。

表结构:

学生表:student(学号,学生姓名,出生年月,性别)

0d5d99960d942c81eb42094c9cfc1642.png

成绩表:score(学号,课程号,成绩)

61e6e023db493f54a3d33716e76249c5.png

课程表:course(课程号,课程名称,教师号)

9c3b79f04d51003178479bd9559de331.png

教师表:teacher(教师号,教师姓名)

5b93e465457ba8e8198a790e11cf30f3.png

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. 行列互换

下面是学生的成绩表(表名score,列名:学号、课程号、成绩)

60916ca9bf4f835d790e78eb7df06129.png

使用sql实现将该表行转列为下面的表结构

c9387105ac16eb79933d7e38ab042403.png

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题及答案解析

表结构

学生表Student

e9bd4a109040f99c89ad92d13b0b8c03.png

教师表Teacher

1c9deac40ac2111067582eafbaa49139.png

课程表Course

89a6794bb2fce35ceb1f2203b4bf2f06.png

成绩表SC

97ee48e09eea98ebd9db6daa207acf3e.png

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

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值