SQL50题
网上看的50道SQL训练题,试着做了一下,难度依次递增。欢迎一起学习的朋友进行讨论指出错误~
数据表介绍
- 学生表
Student(SId,Sname,Sage,Ssex)
SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
create table Student(SId varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10));
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-12-20' , '男');
insert into Student values('04' , '李云' , '1990-12-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-01-01' , '女');
insert into Student values('07' , '郑竹' , '1989-01-01' , '女');
insert into Student values('09' , '张三' , '2017-12-20' , '女');
insert into Student values('10' , '李四' , '2017-12-25' , '女');
insert into Student values('11' , '李四' , '2012-06-06' , '女');
insert into Student values('12' , '赵六' , '2013-06-13' , '女');
insert into Student values('13' , '孙七' , '2014-06-01' , '女');
- 课程表
Course(CId,Cname,TId)
CId 课程编号,Cname 课程名称,TId 教师编号
create table Course(CId varchar(10),Cname nvarchar(10),TId varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
- 教师表
Teacher(TId,Tname)
TId 教师编号,Tname 教师姓名
create table Teacher(TId varchar(10),Tname varchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
- 成绩表
SC(SId,CId,score)
SId 学生编号,CId 课程编号,score 分数
create table SC(SId varchar(10),CId varchar(10),score decimal(18,1));
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);
练习题目
- 查询” 01 “课程比” 02 “课程成绩高的学生的信息及课程分数
select * from (
select s1.SId,s1.score class1,s2.score class2 from test.sc s1
left join test.sc s2 on s1.SId = s2.SId
where s1.CId = "01" and s2.CId = "02" and s1.score > s2.score
)r left join test.student s on r.SId = s.SId;
- 查询同时存在” 01 “课程和” 02 “课程的情况
select s1.SID,s1.score class1,s2.score class2 from test.sc s1
left join test.sc s2 on s1.SId = s2.SId
where s1.CId = "01" and s2.CId = "02"
select * from
(select * from test.sc where CId = "01") t1,
(select * from test.sc where CId = "02") t2
where t1.SId = t2.SId;
- 查询存在” 01 “课程但可能不存在” 02 “课程的情况(不存在时显示为 null )
select * from
(select * from test.sc where CId = "01") t1
left join
(select * from test.sc where CId = "02") t2
on t1.SId = t2.SId;
- 查询不存在” 01 “课程但存在” 02 “课程的情况
select s1.* from test.sc s1
join test.sc s2 on s1.SId = s2.SId
where s1.SId not in (select SId from test.sc s where s.CId = "01")
AND s2.CId = "02";
和网上有些看到的稍微不太一样,我认为不存在01,存在02,是筛选出这个学生的SId,该学生其他课程应该也应展示出来。网上常见其他答案参考如下,仅展示了有02课程:
select *
from sc
where sc.SId not in (select SId from sc where sc.CId='01')
and sc.CId='02';
- 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select r.*,s.Sname from
(SELECT SId,AVG(score) FROM test.sc
group by SId having avg(score) > 60) r
left join test.student s on s.SId = r.SId;
select c.SId, s.Sname, avg(c.score)
from test.sc c inner join test.student s on c.SId=s.SId
group by c.SId
having avg(c.score)>=60;
- 查询在 SC 表存在成绩的学生信息
SELECT distinct t.* FROM test.sc s ,test.student t
where s.SId = t.SId;
- 查询所有同学的学生编号、学生姓名、选课总数、所有课程的成绩总和
SELECT t.SId,t.Sname,r.ccount 课程数量,r.csum 课程总分 FROM test.student t ,
(select s.SId,count(s.CId) ccount,sum(s.score) csum from test.sc s
group by s.SId) r where t.SId = r.SId;
要显示没选课的学生(显示为NULL),使用join:
select s.SId, s.Sname, count(c.CId), sum(score)
from test.student s left join test.sc c on s.SId=c.SId
group by c.SId
- 查有成绩的学生信息
select *
from student
where EXISTS(select * from sc where student.SId=sc.SId)
- 查询「李」姓老师的数量
SELECT count(Tid) FROM test.teacher
where Tname like "李%";
- 查询学过「张三」老师授课的同学的信息
select distinct st.* from
(
select s.* from test.sc s,
(
SELECT c.CId FROM test.course c ,
(SELECT TId FROM test.teacher where Tname = "张三") t
where c.TId = t.TId
) cid
where s.CId = cid.CId
)r
join test.student st on st.SId = r.SId;
上面自己写的,一张一张表查询的,效率可能较低。下面是其他人写的,
select distinct s.*
from test.student s
inner join test.sc sc on s.SId=sc.SId
inner join test.course c on sc.CId=c.CId
inner join test.teacher t on c.TId=t.TId
where t.Tname='张三'
- 查询没有学全所有课程的同学的信息
SELECT * FROM test.student t
where t.SId not in
(
select s.SId from test.sc s
group by s.SId
having count(s.CId)>=3
);
其实这里3的来源是自己数出来有3个,实际应该也是查询出数量有多少,3可以替换成(select count(cid) from course)
- 查询至少有一门课与学号为” 01 “的同学所学相同的同学的信息
SELECT distinct s.SId,t.* FROM test.sc s , test.student t
where CId in
(select CId from test.sc where sc.SId='01')
and s.SId = t.SId and s.SId !="01"
- 查询和” 01 “号的同学学习的课程完全相同的其他同学的信息
SELECT distinct t.SId ,t.Sname,t.Sage,t.Ssex
FROM test.sc s ,test.student t,(select CId from test.sc where sc.SId='01') s1
where s.SId = t.SId and t.SId!='01' and s1.CId = s.CId
group by t.SId
having count(S.CId) > 2
- 查询没学过”张三”老师讲授的任一门课程的学生姓名
select * from test.student s
where s.SId not in
(
select sc.SId FROM test.sc as sc
join
(
SELECT c.CId FROM test.course c,test.teacher t
where c.TId = t.TId and t.Tname = "张三"
)r
on sc.CId = r.CId
);
- 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECT s.SId,s.Sname FROM test.student s
join
(
SELECT sc.SId,avg(score) FROM test.sc sc
where sc.score < 60
group by sc.SId
having count(sc.CId)>1
)r
on s.SId = r.SId ;
其他比我简便的做法
select s.SId, s.Sname, avg(sc1.score)
from (select * from test.sc where score<60) sc1, test.student s
where sc1.SId=s.SId
group by sc1.SId
having count(sc1.CId)>1;
- 检索” 01 “课程分数小于 60,按分数降序排列的学生信息
SELECT * FROM test.student sc,
(
select s.SId,s.score from test.sc s
where s.CId = "01" and s.score < 60
)r
where sc.SId = r.SId
order by r.score desc;
我比较习惯有逻辑的写看的比较清晰,网上较多是比较精炼的如下:
select student.*
from student,sc
where sc.CId ='01'
and sc.score<60
and student.SId=sc.SId
- 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
SELECT
*
FROM
test.sc sc2,
(SELECT
sc.SId, AVG(sc.score) average_score
FROM
test.sc sc
GROUP BY sc.SId) r
WHERE
r.SId = sc2.SId
ORDER BY r.average_score DESC;
突然发现了workbench的一个快捷键可以美化代码,哈哈哈,终于可以告别丑陋的书写了。
- 查询各科成绩最高分、最低分和平均分,以如下形式显示:
课程ID | 课程name | 课程人数 | 最高分 | 最低分 | 平均分 | 及格率 | 中等率 | 优良率 | 优秀率 |
---|---|---|---|---|---|---|---|---|---|
CId | Cname | number | max | min | mean | >=60 | 70-80 | 80-90 | 90-100 |
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT
sc.CId,
c.Cname,
COUNT(DISTINCT sc.SId) 课程人数,
AVG(sc.score) 平均分,
MAX(sc.score) 最高分,
MIN(sc.score) 最低分,
COUNT(CASE
WHEN sc.score >= 60 THEN 1
ELSE NULL
END) / COUNT(DISTINCT sc.SId) 及格率,
COUNT(CASE
WHEN sc.score >= 70 AND sc.score < 80 THEN 1
ELSE NULL
END) / COUNT(DISTINCT sc.SId) 中等率,
COUNT(CASE
WHEN sc.score >= 80 AND sc.score < 90 THEN 1
ELSE NULL
END) / COUNT(DISTINCT sc.SId) 优良率,
COUNT(CASE
WHEN sc.score >= 80 AND sc.score < 90 THEN 1
ELSE NULL
END) / COUNT(DISTINCT sc.SId) 优秀率
FROM
test.sc sc,
test.course c
WHERE
c.CId = sc.CId
GROUP BY CId
ORDER BY 课程人数 DESC , sc.CId;
- 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺
SELECT
a.cid, a.sid, a.score,COUNT(b.score) + 1 AS Srank
FROM
test.sc AS a
LEFT JOIN
teSt.sc AS b ON a.score < b.score AND a.CId = b.CId
GROUP BY a.CId , a.SId , a.score
ORDER BY a.CId , Srank ASC;
这题有些难度没有做出来,参考其他人的方法,有点trick。
- 按各科成绩进行排序,并显示排名, Score 重复时合并名次
SELECT
sc.SId, sc.CId, sc.score, tp.ranks
FROM
test.sc
LEFT JOIN
(SELECT
SId,
CId,
(SELECT
COUNT(DISTINCT sc2.score) + 1
FROM
test.sc sc2
WHERE
sc1.CId = sc2.CId
AND sc2.score > sc1.score) ranks
FROM test.sc sc1) tp
ON sc.SId = tp.SId AND sc.CId = tp.CId
ORDER BY sc.CId , ranks
- 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺
select t1.*,@currank:= @currank+1 as rank
from (select sc.SId, sum(score)
from sc
GROUP BY sc.SId
ORDER BY sum(score) desc) as t1,(select @currank:=0) as t
- 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺
select t1.*, case when @fontscore=t1.sumscore then @currank when @fontscore:=t1.sumscore then @currank:=@currank+1 end as rank
from (select sc.SId, sum(score) as sumscore
from sc
GROUP BY sc.SId
ORDER BY sum(score) desc) as t1,(select @currank:=0,@fontscore:=null) as t
- 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
SELECT
c.CId 课程编号,
c.Cname 课程名称,
COUNT(CASE
WHEN sc.score >= 85 THEN 1
ELSE NULL
END) AS '[100,85]人数',
ROUND(COUNT(CASE
WHEN sc.score >= 85 THEN 1
ELSE NULL
END) * 1.0 / COUNT(sc.score),
2) AS '[100,85]占比',
COUNT(CASE
WHEN sc.score >= 70 AND sc.score < 85 THEN 1
ELSE NULL
END) AS '(85,70]人数',
ROUND(COUNT(CASE
WHEN sc.score >= 70 AND sc.score < 85 THEN 1
ELSE NULL
END) * 1.0 / COUNT(sc.score),
2) AS '(85,70]占比',
COUNT(CASE
WHEN sc.score >= 60 AND sc.score < 70 THEN 1
ELSE NULL
END) AS '(70,60]人数',
ROUND(COUNT(CASE
WHEN sc.score >= 60 AND sc.score < 70 THEN 1
ELSE NULL
END) * 1.0 / COUNT(sc.score),
2) AS '(70,60]占比',
COUNT(CASE
WHEN sc.score < 60 THEN 1
ELSE NULL
END) AS '(60,0]人数',
ROUND(COUNT(CASE
WHEN sc.score < 60 THEN 1
ELSE NULL
END) * 1.0 / COUNT(sc.score),
2) AS '(60,0]占比'
FROM
Course c
LEFT JOIN sc
ON c.CId = sc.CId
GROUP BY c.CId
- 查询各科成绩前三名的记录
select *
from sc
where (select count(*) from sc as a where sc.CId =a.CId and sc.score <a.score )<3
ORDER BY CId asc,sc.score desc
这个就是前三名转化为若大于此成绩的数量少于3即为前三名。
- 查询每门课程被选修的学生数
SELECT
CId, COUNT(SId)
FROM
test.sc
GROUP BY CId
;
- 查询出只选修两门课程的学生学号和姓名
SELECT
s.SId, s.Sname
FROM
test.student s,
test.sc sc
WHERE
s.SId = sc.SId
GROUP BY s.SId
HAVING COUNT(sc.CId) = 2
;
- 查询男生、女生人数
SELECT
Ssex, COUNT(SId)
FROM
test.student
GROUP BY Ssex
;
- 查询名字中含有「风」字的学生信息
SELECT
*
FROM
test.student
WHERE
Sname LIKE '%风%'
;
- 查询同名学生名单,并统计同名人数
SELECT
Sname, COUNT(Sname)
FROM
test.student
GROUP BY Sname
HAVING COUNT(Sname) > 1;
- 查询 1990 年出生的学生名单
SELECT
*
FROM
test.student
WHERE
YEAR(student.Sage) = 1990;
- 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
SELECT
CId, AVG(score) avg_score
FROM
test.sc
GROUP BY CId
ORDER BY avg_score DESC , CId;
- 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
SELECT
sc.SId, s.Sname, AVG(sc.score) avg_score
FROM
test.sc sc,
test.student s
WHERE
sc.SId = s.SId
GROUP BY sc.SId
HAVING SUM(score) >= 85;
- 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
SELECT
c.Cname, s.Sname, sc.score
FROM
test.sc sc,
test.course c,
test.student s
WHERE
sc.CId = c.CId
AND c.Cname = '数学'
AND sc.SId = s.SId
AND sc.score < 60;
- 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
SELECT
s.SId, s.Sname, sc.CId, sc.score
FROM
test.student s
LEFT JOIN
test.sc sc
ON
sc.SId = s.SId;
- 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
SELECT
s.Sname, c.Cname, sc.score
FROM
test.sc sc,
test.student s,
test.course c
WHERE
sc.score > 70
AND sc.SId = s.SId
AND c.CId = sc.CId;
- 查询存在不及格的课程
SELECT DISTINCT
c.Cname
FROM
test.sc sc,
test.course c
WHERE
sc.score < 60
- 查询课程编号为 01 且课程成绩在 80 分及以上的学生的学号和姓名
SELECT DISTINCT
s.SId, s.Sname
FROM
test.sc sc,
test.student s
WHERE
sc.score >= 80
AND sc.CId = '01'
AND sc.SId = s.SId;
- 求每门课程的学生人数
SELECT
sc.CId, COUNT(*) AS 学生人数
FROM
test.sc
GROUP BY sc.CId;
- 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
SELECT DISTINCT
s.*, sc.score
FROM
test.teacher t,
test.sc sc,
test.student s,
test.course c
WHERE
t.TId = c.TId
AND t.Tname = '张三'
AND c.CId = sc.CId
AND s.SId = sc.SId
ORDER BY sc.score DESC
LIMIT 1;
- 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
SELECT
student.*, sc.score, sc.cid
FROM
student,
teacher,
course,
sc
WHERE
teacher.tid = course.tid
AND sc.sid = student.sid
AND sc.cid = course.cid
AND teacher.tname = '张三'
AND sc.score = (
SELECT MAX(sc.score)
FROM
sc,
student,
teacher,
course
WHERE
teacher.tid = course.tid
AND sc.sid = student.sid
AND sc.cid = course.cid
AND teacher.tname = '张三');
- 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
SELECT
sc1.SId, sc1.CId, sc1.score
FROM
test.sc sc1
JOIN
test.sc sc2
ON sc1.SId = sc2.SId
AND sc1.score = sc2.score
AND sc1.CId != sc2.CId
GROUP BY sc1.CId , sc1.SId;
- 查询每门功成绩最好的前两名
SELECT
a.CId, a.SId, a.score
FROM
test.sc a
LEFT JOIN
test.sc b
ON
a.CId = b.CId
AND a.score < b.score
GROUP BY a.CId , a.SId
HAVING COUNT(a.CId) < 2
ORDER BY CId , score DESC
同19题类似的思路
- 统计每门课程的学生选修人数(超过 5 人的课程才统计)。
SELECT a.CId, count(a.SId) FROM test.sc a
GROUP BY a.CId
HAVING COUNT(a.SId) > 5;
- 检索至少选修两门课程的学生学号
SELECT
sc.SId, COUNT(CId) AS cn
FROM
test.sc
GROUP BY SId
HAVING cn > 1;
- 查询选修了全部课程的学生信息
SELECT
sc.SId, COUNT(CId) AS cn
FROM
test.sc
GROUP BY SId
HAVING cn = (SELECT COUNT(cid) FROM test.course);
查询各学生的年龄,只按年份来算
同41按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
SELECT
*,
TIMESTAMPDIFF(YEAR,student.Sage,CURDATE()) age
FROM
test.student;
mysql的timestampdiff函数很好用,满足以上需要
42. 查询本周过生日的学生
SELECT
*
FROM
test.student
WHERE
WEEKOFYEAR(student.Sage) = WEEKOFYEAR(CURDATE());
- 查询下周过生日的学生
SELECT
*
FROM
test.student
WHERE
WEEKOFYEAR(student.Sage) - WEEKOFYEAR(CURDATE()) = 1;
- 查询本月过生日的学生
SELECT
*
FROM
test.student
WHERE
MONTH(student.Sage) = MONTH(CURDATE());
- 查询下月过生日的学生
SELECT
*
FROM
test.student
WHERE
MONTH(student.Sage) = MONTH(CURDATE())+1;
终于写完了,前9题就是基本的操作,并且难度逐渐增加的。而之后主要就是熟悉各种函数,做完后除了对变量还不是很熟悉以外,其他感觉还不错。果然实践做题才是学习sql最好的方式。