今天是是关于44道经典SQL测试题
01 建表语句
1create table Student(sid varchar(10),sname varchar(10),sage datetime,ssex nvarchar(10));
2insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
3insert into Student values('02' , '钱电' , '1990-12-21' , '男');
4insert into Student values('03' , '孙风' , '1990-05-20' , '男');
5insert into Student values('04' , '李云' , '1990-08-06' , '男');
6insert into Student values('05' , '周梅' , '1991-12-01' , '女');
7insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
8insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
9insert into Student values('08' , '王菊' , '1990-01-20' , '女');
10create table Course(cid varchar(10),cname varchar(10),tid varchar(10));
11insert into Course values('01' , '语文' , '02');
12insert into Course values('02' , '数学' , '01');
13insert into Course values('03' , '英语' , '03');
14create table Teacher(tid varchar(10),tname varchar(10));
15insert into Teacher values('01' , '张三');
16insert into Teacher values('02' , '李四');
17insert into Teacher values('03' , '王五');
18create table SC(sid varchar(10),cid varchar(10),score decimal(18,1));
19insert into SC values('01' , '01' , 80);
20insert into SC values('01' , '02' , 90);
21insert into SC values('01' , '03' , 99);
22insert into SC values('02' , '01' , 70);
23insert into SC values('02' , '02' , 60);
24insert into SC values('02' , '03' , 80);
25insert into SC values('03' , '01' , 80);
26insert into SC values('03' , '02' , 80);
27insert into SC values('03' , '03' , 80);
28insert into SC values('04' , '01' , 50);
29insert into SC values('04' , '02' , 30);
30insert into SC values('04' , '03' , 20);
31insert into SC values('05' , '01' , 76);
32insert into SC values('05' , '02' , 87);
33insert into SC values('06' , '01' , 31);
34insert into SC values('06' , '03' , 34);
35insert into SC values('07' , '02' , 89);
36insert into SC values('07' , '03' , 98);
02 表结构预览
--学生表
Student(SId,Sname,Sage,Ssex)
--SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
--课程表
Course(CId,Cname,TId)
--CId 课程编号,Cname 课程名称,TId 教师编号
--教师表
Teacher(TId,Tname)
--TId 教师编号,Tname 教师姓名
--成绩表
SC(SId,CId,score)
--SId 学生编号,CId 课程编号,score 分数
1. 查询“01”课程比“02”课程成绩高的所有学生的学号;
1select distinct t1.sid as sid from
2 (select * from sc where cid='01')t1
3left join
4 (select * from sc where cid='02')t2
5on t1.sid=t2.sid
6where t1.score>t2.score
2. 查询平均成绩大于60分的同学的学号和平均成绩;
1select
2 sid
3 ,avg(score)
4from sc
5group by sid
6having avg(score)>60
3. 查询所有同学的学号、姓名、选课数、总成绩
1select
2 student.sid as sid
3 ,sname
4 ,count(distinct cid) course_cnt
5 ,sum(score) as total_score
6from student
7left join sc
8on student.sid=sc.sid
9group by sid,sname
4. 查询姓“李”的老师的个数;
1select
2 count(distinct tid) as teacher_cnt
3from teacher
4where tname like '李%'
5. 查询没学过“张三”老师课的同学的学号、姓名;
1select
2 sid,sname
3from student
4where sid not in
5 (
6 select
7 sc.sid
8 from teacher
9 left join course
10 on teacher.tid=course.tid
11 left join sc
12 on course.cid=sc.cid
13 where teacher.tname='张三'
14 )
6. 查询学过“01”并且也学过编号“02”课程的同学的学号、姓名;
1select
2 t.sid as sid
3 ,sname
4from
5 (
6 select
7 sid
8 ,count(if(cid='01',score,null)) as count1
9 ,count(if(cid='02',score,null)) as count2
10 from sc
11 group by sid
12 having count(if(cid='01',score,null))>0 and count(if(cid='02',score,null))>0
13 )t
14left join student
15 on t.sid=student.sid
7. 查询学过“张三”老师所教的课的同学的学号、姓名;
1select
2 student.sid
3 ,sname
4from
5 (
6 select
7 distinct cid
8 from course
9 left join teacher
10 on course.tid=teacher.tid
11 where teacher.tname='张三'
12 )course
13left join sc
14 on course.cid=sc.cid
15left join student
16 on sc.sid=student.sid
17group by student.sid,sname
8. 查询课程编号“01”的成绩比课程编号“02”课程低的所有同学的学号、姓名;
1select
2 t1.sid,sname
3from
4 (
5 select distinct t1.sid as sid
6 from
7 (select * from sc where cid='01')t1
8 left join
9 (select * from sc where cid='02')t2
10 on t1.sid=t2.sid
11 where t1.score>t2.score
12 )t1
13left join student
14 on t1.sid=student.sid
9. 查询所有课程成绩小于60分的同学的学号、姓名;
1select
2 t1.sid,sname
3from
4 (
5 select
6 sid,max(score)
7 from sc
8 group by sid
9 having max(score<60)
10 )t1
11left join student
12 on t1.sid=student.sid
10. 查询没有学全所有课的同学的学号、姓名;
1select
2 t1.sid,sname
3from
4 (
5 select
6 count(cid),sid
7 from sc
8 group by sid
9 having count(cid) < (select count(distinct cid) from course)
10 )t1
11left join student
12 on t1.sid=student.sid
11. 查询至少有一门课与学号为“01”的同学所学相同的同学的学号和姓名;
1select
2 distinct sc.sid
3from
4 (
5 select
6 cid
7 from sc
8 where sid='01'
9 )t1
10left join sc
11 on t1.cid=sc.cid
12. 查询和"01"号的同学学习的课程完全相同的其他同学的学号和姓名
1#注意是和'01'号同学课程完全相同但非学习课程数相同的,这里我用左连接解决这个问题select
2 t1.sid,sname
3from
4 (
5 select
6 sc.sid
7 ,count(distinct sc.cid)
8 from
9 (
10 select
11 cid
12 from sc
13 where sid='01'
14 )t1 #选出01的同学所学的课程
15 left join sc
16 on t1.cid=sc.cid
17 group by sc.sid
18 having count(distinct sc.cid)= (select count(distinct cid) from sc where sid = '01')
19 )t1
20left join student
21 on t1.sid=student.sid
22where t1.sid!='01'
13. 把“SC”表中“张三”老师教的课的成绩都更改为此课程的平均成绩;
1#暂跳过update题目
14. 查询没学过"张三"老师讲授的任一门课程的学生姓名
1select
2 sname
3from student
4where sid not in
5 (
6 select
7 distinct sid
8 from sc
9 left join course
10 on sc.cid=course.cid
11 left join teacher
12 on course.tid=teacher.tid
13 where tname='张三'
14 )
15. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
1select
2 t1.sid,sname,avg_score
3from
4 (
5 select
6 sid,count(if(score<60,cid,null)),avg(score) as avg_score
7 from sc
8 group by sid
9 having count(if(score<60,cid,null)) >=2
10 )t1
11left join student
12 on t1.sid=student.sid
16. 检索"01"课程分数小于60,按分数降序排列的学生信息
1select
2 sid,if(cid='01',score,100)from sc
3where if(cid='01',score,100)<60
4order by if(cid='01',score,100) desc
17. 按平均成绩从高到低显示所有学生的平均成绩
1select sid,avg(score)
2from sc
3group by sid
4order by avg(score) desc
18. 查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率
1select
2 sc.cid
3 ,cname
4 ,max(score) as max_score
5 ,min(score) as min_score
6 ,avg(score) as avg_score
7 ,count(if(score>=60,sid,null))/count(sid) as pass_rate
8 from sc
9 left join course
10 on sc.cid=course.cid
11 group by sc.cid
19. 按各科平均成绩从低到高和及格率的百分数从高到低顺序
1#这里先按照平均成绩排序,再按照及格百分数排序,
2select
3 cid
4 ,avg(score) as avg_score
5 ,count(if(score>=60,sid,null))/count(sid) as pass_rate
6from sc
7group by cid
8order by avg_score,pass_rate desc
20. 查询学生的总成绩并进行排名
1select
2 sid
3 ,sum(score) as sum_score
4from sc
5group by sid
6order by sum_score desc
21. 查询不同老师所教不同课程平均分从高到低显示
1select
2 tid
3 ,avg(score) as avg_score
4from course
5left join sc
6 on course.cid=sc.cid
7group by tid
8order by avg_score desc
22. 查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
1select
2 sid,rank_num,score,cid
3from
4 (
5 select
6 rank() over(partition by cid order by score desc) as rank_num
7 ,sid
8 ,score
9 ,cid
10 from sc
11 )t
12where rank_num in (2,3)
23. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
1select
2 sc.cid
3 ,cname
4 ,count(if(score between 85 and 100,sid,null))/count(sid)
5 ,count(if(score between 70 and 85,sid,null))/count(sid)
6 ,count(if(score between 60 and 70,sid,null))/count(sid)
7 ,count(if(score between 0 and 60,sid,null))/count(sid)
8from sc
9left join course
10 on sc.cid=course.cid
11group by sc.cid,cname
24. 查询学生平均成绩及其名次
1select
2 sid
3 ,avg_score
4 ,rank() over (order by avg_score desc)
5from
6 (
7 select
8 sid
9 ,avg(score) as avg_score
10 from sc
11 group by sid
12 )t
25. 查询各科成绩前三名的记录
1select
2 sid,cid,rank1from
3 (
4 select
5 cid
6 ,sid
7 ,rank() over(partition by cid order by score desc) as rank1
8 from sc
9 )twhere rank1<=3
26. 查询每门课程被选修的学生数
1select
2 count(sid)
3 ,cid
4from sc
5group by cid
27. 查询出只选修了一门课程的全部学生的学号和姓名
1select
2 sid
3from sc
4group by sid
5having count(cid) =1
28. 查询男生、女生人数
1select
2 ssex
3 ,count(distinct sid)from studentgroup by ssex
29. 查询名字中含有"风"字的学生信息
1select
2 sid,sname
3from student
4where sname like '%风%'
30. 查询同名同性学生名单,并统计同名人数
1select
2 ssex
3 ,sname
4 ,count(sid)
5from student
6group by ssex,sname
7having count(sid)>=2
31. 查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime)
1select
2 sid,sname,sage
3from student
4where year(sage)=1990
32. 查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
1select
2 cid,avg(score) as avg_score
3from sc
4group by cid
5order by avg_score,cid desc
33. 查询不及格的课程,并按课程号从大到小排列
1select
2 cid,sid,score
3from sc
4where score<60
5order by cid desc,sid
34. 查询课程编号为"01"且课程成绩在60分以上的学生的学号和姓名;
1select
2 sid,cid,scorefrom scwhere cid='01' and score>60
35. 查询选修“张三”老师所授课程的学生中,成绩最高的学生姓名及其成绩
1select
2 sc.sid,sname,cname,score
3from sc
4left join course
5 style="font-weight: 600;">=course.cid
6left join teacher
7 style="font-weight: 600;">=teacher.tid
8left join student
9 style="font-weight: 600;">=student.sid
10where tname='张三'
11order by score desc
12limit 1;
36. 查询每门功课成绩最好的前两名
1select
2 cid,sid,rank1
3from
4 (
5 select
6 cid
7 ,sid
8 ,rank() over(partition by cid order by score desc) as rank1
9 from sc
10 )t
11where rank1 <=2
37. 统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
1select
2 cid
3 ,count(sid) as cnt
4from sc
5group by cid
6having cnt>=5
7order by count(sid) desc,cid
38. 检索至少选修两门课程的学生学号
1select
2 sid
3 ,count(cid)
4from sc
5group by sid
6having count(cid)>=2
39. 查询选修了全部课程的学生信息
1select
2 sid
3 ,count(cid)
4from sc
5group by sid
6having count(cid)=(select count(distinct cid) from sc)
40. 查询各学生的年龄
1select
2 sid,sname,year(curdate())-year(sage) as sage
3from student
41 查询本周过生日的学生
1select
2 sid,sname,sage
3from student
4where weekofyear(sage)=weekofyear(curdate())
42. 查询下周过生日的学生
1select
2 sid,sname,sage
3from student
4where weekofyear(sage) = weekofyear(date_add(curdate(),interval 1 week))
43 查询本月过生日的学生
1select
2 sid,sname,sage
3from student
4where month(sage) = month(curdate())
44. 查询下月过生日的学生
1select
2 sid,sname,sage
3from student
4where month(date_sub(sage,interval 1 month)) = month(curdate())
End.
作者:tomocat
来源:知乎
更多推荐
SQL | 数据分析面试必备SQL语句+语法
总结了28道数据分析经典面试题
干货 | 50题带你入门Python数据分析(下)干货 | 50题带你入门Python数据分析(上)
长按二维码
关注我们吧
看都看完了,还不点这里试试