超经典SQL练习题,面试数据分析师必备!
2020-05-15 本文已影响0人
数据蝉
一、测试表设计
--1.学生表
Student(Stid,Sname,Sage,Ssex)
--Stid 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别--2.课程表
Course(Cid,Cname,Tid)
--Cid --课程编号,Cname 课程名称,Tid 教师编号--3.教师表
Teacher(Tid,Tname)
--Tid 教师编号,Tname 教师姓名--4.成绩表
SC(Scid,Cid,score)
--Scid 学生编号,Cid 课程编号,score 分数
二、创建测试数据
2.1 学生表 Student
create table `Student`(
`Sid` varchar(10) not null,
`Sname` varchar(10) DEFAULT null ,
`Sage` datetime DEFAULT null,
`Ssex` varchar(10) DEFAULT null
)ENGINE=INNODB DEFAULT CHARSET=utf8;
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' , '女');
2.2 科目表 Course
create table `Course`(
`Cid` varchar(10) not null,
`Cname` varchar(10) DEFAULT null ,
`Tid` varchar(10) DEFAULT null
)ENGINE=INNODB DEFAULT CHARSET=utf8;
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
2.3 教师表 Teacher
create table `Teacher`
(`Tid` varchar(10) not null,
`Tname` nvarchar(10) DEFAULT null
)ENGINE=INNODB DEFAULT CHARSET=utf8;
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
2.4 成绩表 SC
create table `SC`
(`Sid` varchar(10) not null,
`Cid` varchar(10) DEFAULT null,
`score` decimal(18,1) DEFAULT null
)ENGINE=INNODB DEFAULT CHARSET=utf8;
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);
三、50道练习题
1. 查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
select A.*,B.Cid,B.score
from
(select * from SC where Cid='01') A
left join
(select * from SC where Cid='02') B
on A.Sid=B.Sid
where A.score>B.score
2.查询同时存在" 01 "课程和" 02 "课程的情况
select * from
(select * from SC where Cid='01') A
left join
(select * from SC where Cid='02') B
on A.Sid=B.Sid
where B.Sid is not null
3.查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为null)
select * from
(select * from SC where Cid='01') A
left join
(select * from SC where Cid='02') B
on A.Sid=B.Sid
4.查询不存在" 01 "课程但存在" 02 "课程的情况
select * from SC
where Cid='02'
and Sid not in(select Sid from SC where Cid='01')
6. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select A.Sid,B.Sname,A.dc
from (select Sid,AVG(score) dc from SC group by Sid) A
left join Student B
on A.Sid=B.Sid
where A.dc>=60
7. 查询在 SC 表存在成绩的学生信息
select * from Student where Sid in (select distinct Sid from SC)
8. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为null)
select B.Sid,B.Sname,A.选课总数,A.总成绩 from
(select Sid,COUNT(Cid) 选课总数,sum(score) 总成绩 from SC group by Sid) A
right join Student B
on A.Sid=B.Sid
9.查有成绩的学生信息
select A.Sid,B.Sname,A.选课总数,A.总成绩 from
(select Sid,COUNT(Cid) 选课总数,sum(score) 总成绩 from SC group by Sid) A
left join Student B
on A.Sid=B.Sid
10..查询「李」姓老师的数量
select COUNT(*) 李姓老师数量 from Teacher where Tname like '李%'
11..查询学过「张三」老师授课的同学的信息
select * from Student
where Sid in(select distinct Sid from SC
where Cid in (select Cid from Course
where Tid in (select Tid from Teacher where Tname='张三')))
12.查询没有学全所有课程的同学的信息
select * from Student where Sid in(select Sid from SC group by Sid having COUNT(Cid)<3)
13. 查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
select * from Student
where Sid in(select distinct Sid from SC where Cid in(select Cid from SC where Sid='01'))
14. 查询和" 01 "号的同学学习的课程完全相同的其他同学的信息
select * from Student
where Sid in(select Sid from SC where Cid in(select distinct Cid from SC where Sid='01') and Sid<>'01'
group by Sid
having COUNT(Cid)>=3)
15. 查询没学过「张三」老师讲授的任一门课程的学生姓名
select Sname from Student
where Sid not in(select Sid from SC
where Cid in(select Cid from Course where Tid in(select Tid from Teacher where Tname='张三')
))
16.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select A.Sid,A.Sname,B.平均成绩 from Student A right join
(select Sid,AVG(score)平均成绩 from SC where score<60 group by Sid having COUNT(score)>=2) B
on A.Sid=B.Sid
17.检索" 01 "课程分数小于 60 ,按分数降序排列的学生信息
select Sid,score from SC where Cid='01' and score<60 order by score desc
18.(静态写法)按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select Sid,max(case Cid when '01' then score else 0 end) '01',
max(case Cid when '02' then score else 0 end)'02',
MAX(case Cid when '03' then score else 0 end)'03',AVG(score)平均分 from SC
group by Sid order by 平均分 desc
19.查询各科成绩最高分、最低分和平均分:
--以如下形式显示:课程 ID ,课程 name ,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
--及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
select distinct A.Cid,Cname,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
from SC A
left join Course
on A.Cid=Course.Cid
left join
(select Cid,MAX(score) 最高分,MIN(score) 最低分,AVG(score) 平均分 from SC group by Cid) B on A.Cid=B.Cid
left join
(select Cid,sum(case when score>=60 then 1 else 0 end)/COUNt(1) 及格率 from SC group by Cid) C
on A.Cid=C.Cid
left join
(select Cid,sum(case when score >=70 and score<80 then 1 else 0 end)/COUNT(1) 中等率 from SC group by Cid) D
on A.Cid=D.Cid
left join (select Cid,sum(case when score >=80 and score<90 then 1 else 0 end)/COUNT(1) 优良率 from SC group by Cid) E
on A.Cid=E.Cid
left join (select Cid,sum(case when score >=90 then 1 else 0 end)/COUNT(1) 优秀率
from SC group by Cid) F
on A.Cid=F.Cid
20. 按各科成绩进行排序,并显示排名,Score 重复时保留名次空缺
select *,RANK()over(order by score desc) 排名 from SC
21. 按各科成绩进行排序,并显示排名,Score 重复时合并名次
select *,DENSE_RANK()over(order by score desc) 排名 from SC
22. 查询学生的总成绩,并进行排名,总分重复时保留名次空缺
select *,RANK()over(order by 总成绩 desc) 排名
from
(select Sid,SUM(score)总成绩 from SC group by Sid) A
23. 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺
select *,dense_rank()over(order by 总成绩 desc)排名 from(
select Sid,SUM(score)总成绩 from SC group by Sid) A
24. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
select distinct A.Cid,B.Cname,C.`[100-85]`,C.所占百分比,D.`[85-70]`,D.所占百分比,E.`[70-60]`,E.所占百分比,F.`[60-0]`,F.所占百分比
from SC A
left join Course B ON A.Cid=B.Cid
left join (select Cid,sum(case when score>85 and score<=100 then 1 else null end) "[100-85]",
sum(case when score>85 and score<100 then 1 else null end)/COUNT(*) 所占百分比 from SC group by Cid) C
on A.Cid=C.Cid
left join (select Cid,sum(case when score>70 and score<=85 then 1 else null end) "[85-70]",
sum(case when score>70 and score<=85 then 1 else null end)/COUNT(*) 所占百分比 from SC group by Cid) D
on A.Cid=D.Cid
left join (select Cid,sum(case when score>60 and score<=70 then 1 else null end) "[70-60]",
sum(case when score>60 and score<=70 then 1 else null end)/COUNT(*) 所占百分比 from SC group by Cid) E
on A.Cid=E.Cid
left join (select Cid,sum(case when score>0 and score<=60 then 1 else null end) "[60-0]",
sum(case when score>0 and score<=60 then 1 else null end)/COUNT(*) 所占百分比 from SC group by Cid) F
on A.Cid=F.Cid
25. 查询各科成绩前三名的记录(方法 1)
select * from(select *,rank()over (partition by Cid order by score desc) A from SC)B where B.A<=3
26. 查询各科成绩前三名的记录(取 a 的最高分与本表比较)(方法 2)
select a.Sid,a.Cid,a.score from SC a
left join SC b on a.Cid=b.Cid and a.score<b.score
group by a.Sid,a.Cid,a.score
having COUNT(b.Sid)<3
order by a.Cid,a.score desc
27. 查询各科成绩前三名的记录(取 a)(方法 3)
select * from SC a where (select COUNT(*) from SC where Cid=a.Cid and score>a.score)<3
order by a.Cid,a.score desc
28. 查询每门课程被选修的学生数
select Cid,COUNT(Sid) 学生数 from SC group by Cid
29. 查询出只选修两门课程的学生学号和姓名
select Sid,Sname from Student
where Sid in(select Sid from(select Sid,COUNT(Cid) 课程数 from SC group by Sid) A where A.课程数=2)
30. 查询男生、女生人数
select Ssex,COUNT(Ssex)人数 from Student group by Ssex
31. 查询名字中含有「风」字的学生信息
select * from Student where Sname like '%风%'
32. 查询同名同性学生名单,并统计同名人数
select A.*,B.同名人数 from Student A
left join
(select Sname,Ssex,COUNT(*) 同名人数
from Student group by Sname,Ssex) B
on A.Sname=B.Sname and A.Ssex=B.Ssex
where B.同名人数>1
33.查询 1990 年出生的学生名单
select * from Student where YEAR(Sage)=1990
34. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select Cid,AVG(score)平均成绩 from SC group by Cid order by 平均成绩 desc,Cid
35. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
select A.Sid,A.Sname,B.平均成绩 from Student A
left join (select Sid,AVG(score)平均成绩 from SC group by Sid)B on A.Sid=B.Sid
where B.平均成绩>85
36. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
select B.Sname,A.score from(select * from SC where score<60 and Cid in (select Cid from Course where Cname='数学'))A
left join Student B on A.Sid=B.Sid
37. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
select A.Sid,B.Cid,B.score from Student A left join SC B on A.Sid=B.Sid
38. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
select A.Sname,D.Cname,D.score from
(select B.*,C.Cname from(select * from SC where score>70)B left join Course C on B.Cid=C.Cid)D
left join Student A on D.Sid=A.Sid
39. 查询不及格的课程
select * from SC where score<60
40. 查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
select A.Sid,B.Sname from (select * from SC where score>80 and Cid=01)A
left join Student B on A.Sid=B.Sid
41. 求每门课程的学生人数
select Cid,COUNT(*)学生人数 from SC group by Cid
42. 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
select top 1* from SC
where Cid in(select Cid from Course where Tid in(select Tid from Teacher where Tname='张三'))
order by score desc
43. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
select *from(select *,DENSE_RANK()over (order by score desc)A
from SC
where Cid=(select Cid from Course where Tid=(select Tid from Teacher where Tname='张三')))B
where B.A=1
44. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
select C.Sid,max(C.Cid) Cid,max(C.score) score from SC C
left join(select Sid,avg(score)A from SC group by Sid) B
on C.Sid=B.Sid
where C.score=B.A
group by C.Sid
having COUNT(0)=(select COUNT(0)from SC where Sid=C.Sid)
45. 查询每门功成绩最好的前两名
select * from
(select *,ROW_NUMBER()over(partition by Cid order by score desc)A from SC)B
where B.A<3
46.统计每门课程的学生选修人数(超过5人的课程才统计)。
--要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select Cid,COUNT(Sid)选修人数 from SC
group by Cid
having COUNT(Sid)>5
order by 选修人数 desc,Cid
47. 检索至少选修两门课程的学生学号
select Sid from SC
group by Sid
having COUNT(Cid)>=2
48. 查询选修了全部课程的学生信息
select Sid from SC
group by Sid
having count(Cid)=(select distinct COUNT(0)a from Course)
49. 查询各学生的年龄,只按年份来算
select Sid,TIMESTAMPDIFF(YEAR,Sage,DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%S'))年龄 from Student
50.查询7天内生日的学生
select * from Student
where DATE_FORMAT(Sage,'%m-%d') >= DATE_FORMAT(now(),'%m-%d')
and DATE_FORMAT(Sage,'%m-%d') <= DATE_FORMAT(date_add(now(), INTERVAL 6 DAY),'%m-%d')