超经典SQL练习题(MySQL版),你还怕SQL不过关吗?

440次阅读  |  发布于3年以前

@

1 数据表

1.1 学生表 Student(SId,Sname,Sage,Ssex)

SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别

1.2 课程表 Course(CId,Cname,TId)

CId 课程编号,Cname 课程名称,TId 教师编号

1.3 教师表 Teacher(TId,Tname)

TId 教师编号,Tname 教师姓名

1.4 成绩表 SC(SId,CId,score)

SId 学生编号,CId 课程编号,score 分数

2 SQL语句

2.1 学生表 SQL


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','女');

2.2 科目表 SQL

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');

2.3 教师表 SQL

create table Teacher(TId varchar (10),Tname varchar (10));
insert into Teacher values ('01','张三');
insert into Teacher values ('02','李四');
insert into Teacher values ('03','王五');

2.4 成绩表 SQL

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 );

3 练习题

3.1 查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数

因为需要全部的学生信息,则需要在 SC 表中得到符合条件的 SId 后与 Student 表进行 join,可以 left join 也可以 right join。

# right join
SELECT *
FROM Student
 RIGHT JOIN (
  SELECT t1.SId, class1, class2
  FROM (
   SELECT SId, score AS class1
   FROM sc
   WHERE sc.CId = '01'
  ) t1, (
    SELECT SId, score AS class2
    FROM sc
    WHERE sc.CId = '02'
   ) t2
  WHERE t1.SId = t2.SId
   AND t1.class1 > t2.class2
 ) r
 ON Student.SId = r.SId;

# left join
SELECT *
FROM (
 SELECT t1.SId, class1, class2
 FROM (
  SELECT SId, score AS class1
  FROM sc
  WHERE sc.CId = '01'
 ) t1, (
   SELECT SId, score AS class2
   FROM sc
   WHERE sc.CId = '02'
  ) t2
 WHERE t1.SId = t2.SId
  AND t1.class1 > t2.class2
) r
 LEFT JOIN Student ON Student.SId = r.SId;

3.2 查询同时存在" 01 "课程和" 02 "课程的情况

SELECT *
FROM (
 SELECT *
 FROM sc
 WHERE sc.CId = '01'
) t1, (
  SELECT *
  FROM sc
  WHERE sc.CId = '02'
 ) t2
WHERE t1.SId = t2.SId;

3.3 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null)

这一道需要使用 join 的情况了," 02 " 课程可能不存在,即为 left join 的右侧或 right join 的左侧即可。

SELECT *
FROM (
 SELECT *
 FROM sc
 WHERE sc.CId = '01'
) t1
 LEFT JOIN (
  SELECT *
  FROM sc
  WHERE sc.CId = '02'
 ) t2
 ON t1.SId = t2.SId;
SELECT *
FROM (
 SELECT *
 FROM sc
 WHERE sc.CId = '02'
) t2
 RIGHT JOIN (
  SELECT *
  FROM sc
  WHERE sc.CId = '01'
 ) t1
 ON t1.SId = t2.SId;

3.4 查询不存在" 01 "课程但存在" 02 "课程的情况

SELECT *
FROM sc
WHERE sc.SId NOT IN (
  SELECT SId
  FROM sc
  WHERE sc.CId = '01'
 )
 AND sc.CId = '02';

3.5 查询平均成绩大于等于 60 分的同学的学生编号、学生姓名、平均成绩。

这里只需根据学生 ID 把成绩分组,对分组中的 score 求平均值,最后在选取结果中 AVG 大于 60 的即可。

注意,必须要给计算得到的 AVG 结果一个 alias(AS ss),得到学生信息的时候既可以用 join 也可以用一般的联合搜索。

SELECT student.SId, sname, ss
FROM student, (
  SELECT SId, AVG(score) AS ss
  FROM sc
  GROUP BY SId
  HAVING AVG(score) > 60
 ) r
WHERE student.sid = r.sid;
SELECT Student.SId, Student.Sname, r.ss
FROM Student
 RIGHT JOIN (
  SELECT SId, AVG(score) AS ss
  FROM sc
  GROUP BY SId
  HAVING AVG(score) > 60
 ) r
 ON Student.SId = r.SId;
SELECT s.SId, ss, Sname
FROM (
 SELECT SId, AVG(score) AS ss
 FROM sc
 GROUP BY SId
 HAVING AVG(score) > 60
) r
 LEFT JOIN (
  SELECT Student.SId, Student.Sname
  FROM Student
 ) s
 ON s.SId = r.SId;

3.6 查询所有同学的学生编号、学生姓名、选课总数、所有课程的成绩总和联合查询没选课的学生

SELECT student.sid, student.sname, r.coursenumber, r.scoresum
FROM student, (
  SELECT sc.sid, sum(sc.score) AS scoresum, count(sc.cid) AS coursenumber
  FROM sc
  GROUP BY sc.sid
 ) r
WHERE student.sid = r.sid;

如要显示没选课的学生(显示为 NULL),需要使用 join:

SELECT s.sid, s.sname, r.coursenumber, r.scoresum
FROM (
 SELECT student.sid, student.sname
 FROM student
) s
 LEFT JOIN (
  SELECT sc.sid, sum(sc.score) AS scoresum, count(sc.cid) AS coursenumber
  FROM sc
  GROUP BY sc.sid
 ) r
 ON s.sid = r.sid;

3.7 查有成绩的学生信息

这一题涉及到 in 和 exists 的用法,在这种小表中,两种方法的效率都差不多,但是请参考 SQL 查询中 in 和 exists 的区别分析,当表2的记录数量非常大的时候,选用 exists 比 in 要高效很多。exists 用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值 True 或 False。

结论:IN() 适合B表比A表数据小的情况

结论:EXISTS() 适合B表比A表数据大的情况

SELECT *
FROM student
WHERE EXISTS (
 SELECT sc.sid
 FROM sc
 WHERE student.sid = sc.sid
);
SELECT *
FROM student
WHERE student.sid IN (
 SELECT sc.sid
 FROM sc
);

3.8 查询「李」姓老师的数量

SELECT count(*)
FROM teacher
WHERE tname LIKE '李%';

3.9 查询学过「张三」老师授课的同学的信息,多表联合查询

SELECT student.*
FROM student, teacher, course, sc
WHERE student.sid = sc.sid
 AND course.cid = sc.cid
 AND course.tid = teacher.tid
 AND tname = '张三';

3.10 查询没有学全所有课程的同学的信息

因为有学生什么课都没有选,反向思考,先查询选了所有课的学生,再选择这些人之外的学生。

SELECT *
FROM student
WHERE student.sid NOT IN (
 SELECT sc.sid
 FROM sc
 GROUP BY sc.sid
 HAVING count(sc.cid) = (
  SELECT count(cid)
  FROM course
 )
);

3.11 查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息

这个用联合查询也可以,但是逻辑不清楚,我觉得较为清楚的逻辑是这样的:从 sc 表查询 01 同学的所有选课 cid -- 从 sc 表查询所有同学的 sid 如果其 cid 在前面的结果中 -- 从 student 表查询所有学生信息如果sid在前面的结果中。

SELECT *
FROM student
WHERE student.sid IN (
 SELECT sc.sid
 FROM sc
 WHERE sc.cid IN (
  SELECT sc.cid
  FROM sc
  WHERE sc.sid = '01'
 )
);

3.12 查询没学过"张三"老师讲授的任一门课程的学生姓名

仍然还是嵌套,三层嵌套, 或者多表联合查询:

SELECT *
FROM student
WHERE student.sid NOT IN (
 SELECT sc.sid
 FROM sc
 WHERE sc.cid IN (
  SELECT course.cid
  FROM course
  WHERE course.tid IN (
   SELECT teacher.tid
   FROM teacher
   WHERE tname = '张三'
  )
 )
);
SELECT *
FROM student
WHERE student.sid NOT IN (
 SELECT sc.sid
 FROM sc, course, teacher
 WHERE sc.cid = course.cid
  AND course.tid = teacher.tid
  AND teacher.tname = '张三'
);

3.13 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

从 SC 表中选取 score 小于 60 的,并 group by sid,having count 大于 1。

SELECT student.sid, student.sname, AVG(sc.score)
FROM student, sc
WHERE student.sid = sc.sid
 AND sc.score < 60
GROUP BY sc.sid
HAVING count(*) > 1;

3.14 检索" 01 "课程分数小于 60 ,按分数降序排列的学生信息

双表联合查询,在查询最后可以设置排序方式,语法为 ORDERBY*****DESC\ASC。

SELECT student.*, sc.score
FROM student, sc
WHERE student.sid = sc.sid
 AND sc.score < 60
 AND cid = '01'
ORDER BY sc.score DESC;

3.15 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

SELECT *
FROM sc
 LEFT JOIN (
  SELECT sid, avg(score) AS avscore
  FROM sc
  GROUP BY sid
 ) r
 ON sc.sid = r.sid
ORDER BY avscore DESC;

3.16 查询各科成绩最高分、最低分和平均分

以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率。

及格为>= 60 ,中等为:70 - 80 ,优良为:80 - 90 ,优秀为:>= 90

要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列。

SELECT sc.CId, max(sc.score) AS 最高分, min(sc.score) AS 最低分
 , AVG(sc.score) AS 平均分, count(*) AS 选修人数
 , sum(CASE 
  WHEN sc.score >= 60 THEN 1
  ELSE 0
 END) / count(*) AS 及格率
 , sum(CASE 
  WHEN sc.score >= 70
   AND sc.score < 80
  THEN 1
  ELSE 0
 END) / count(*) AS 中等率
 , sum(CASE 
  WHEN sc.score >= 80
   AND sc.score < 90
  THEN 1
  ELSE 0
 END) / count(*) AS 优良率
 , sum(CASE 
  WHEN sc.score >= 90 THEN 1
  ELSE 0
 END) / count(*) AS 优秀率
FROM sc
GROUP BY sc.CId
ORDER BY count(*) DESC, sc.CId ASC

3.17 按各科成绩进行排序,并显示排名, Score重复时保留名次空缺

这一道题可以用变量,但也有更为简单的方法,即自交(左交),用 sc 中的 score 和自己进行对比,来计算“比当前分数高的分数有几个。

SELECT a.cid, a.sid, a.score
 , count(b.score) + 1 AS rank
FROM sc a
 LEFT JOIN sc b
 ON a.score < b.score
  AND a.cid = b.cid
GROUP BY a.cid, a.sid, a.score
ORDER BY a.cid, rank ASC;

3.18 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺

这里主要学习一下使用变量。在 SQL 里面变量用 @ 来标识。

SET @crank = 0;

SELECT q.sid, total, @crank := @crank + 1 AS rank
FROM (
 SELECT sc.sid, sum(sc.score) AS total
 FROM sc
 GROUP BY sc.sid
 ORDER BY total DESC
) q;

3.19 统计各科成绩各分数段人数:课程编号,课程名称,[ 100 - 85 ],[ 85 - 70 ],[ 70 - 60 ],[ 60 - 0 ]及所占百分比

group by 以后的查询结果无法使用别名,所以不要想着先单表 group by 计算出结果再从第二张表里添上课程信息,而应该先将两张表 join 在一起得到所有想要的属性再对这张总表进行统计计算。这里就不算百分比了,道理相同。

注意一下,用 case when 返回 1 以后的统计不是用 count 而是 sum。

SELECT course.cname, course.cid
 , sum(CASE 
  WHEN sc.score <= 100
   AND sc.score > 85
  THEN 1
  ELSE 0
 END) AS "[100-85]"
 , sum(CASE 
  WHEN sc.score <= 85
   AND sc.score > 70
  THEN 1
  ELSE 0
 END) AS "[85-70]"
 , sum(CASE 
  WHEN sc.score <= 70
   AND sc.score > 60
  THEN 1
  ELSE 0
 END) AS "[70-60]"
 , sum(CASE 
  WHEN sc.score <= 60
   AND sc.score > 0
  THEN 1
  ELSE 0
 END) AS "[60-0]"
FROM sc
 LEFT JOIN course ON sc.cid = course.cid
GROUP BY sc.cid;

3.20 查询各科成绩前三名的记录

mysql 不能 group by了以后取 limit,所以不要想着讨巧了。思路有两种,第一种比较暴力,计算比自己分数大的记录有几条,如果小于 3 就 select,因为对前三名来说不会有 3 个及以上的分数比自己大了,最后再对所有 select 到的结果按照分数和课程编号排名即可。

SELECT *
FROM sc
WHERE (
 SELECT count(*)
 FROM sc a
 WHERE sc.cid = a.cid
  AND sc.score < a.score
) < 3
ORDER BY cid ASC, sc.score DESC;

第二种比较灵巧一些,用自身左交,但是有点难以理解。先用自己交自己,条件为 a.cid=b.cidanda.score<b.score,其实就是列出同一门课内所有分数比较的情况。想要查看完整的表可以:

SELECT *
FROM sc a
 LEFT JOIN sc b
 ON a.cid = b.cid
  AND a.score < b.score
ORDER BY a.cid, a.score;

结果查看,发现结果是 47 行的一个表,列出了类似 01 号课里“ 30 分小于 50 ,也小于 70 ,也小于 80 ,也小于 90 ”“ 50 分小于 70 ,小于 80 ,小于 90 ”.....

所以理论上,对任何一门课来说,分数最高的那三个记录,在这张大表里,通过 a.sid 和 a.cid。

可以联合确定这个同学的这门课的这个分数究竟比多少个其他记录高/低,如果这个特定的 a.sid 和 a.cid 组合出现在这张表里的次数少于 3 个,那就意味着这个组合(学号+课号+分数)是这门课里排名前三的。

所以下面这个计算中 having count 部分其实 count() 或者任意其他列都可以,一个列只是因为比 count() 运行速度上更快。

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.cid, a.sid
HAVING count(b.cid) < 3
ORDER BY a.cid;

3.21 查询每门课程被选修的学生数

SELECT cid, count(sid)
FROM sc
GROUP BY cid;

3.22 查询出只选修两门课程的学生学号和姓名

嵌套查询:

SELECT student.sid, student.sname
FROM student
WHERE student.sid IN (
 SELECT sc.sid
 FROM sc
 GROUP BY sc.sid
 HAVING count(sc.cid) = 2
);

联合查询:

SELECT student.SId, student.Sname
FROM sc, student
WHERE student.SId = sc.SId
GROUP BY sc.SId
HAVING count(*) = 2;

3.23 查询男生、女生人数

SELECT ssex, count(*)
FROM student
GROUP BY ssex;

3.24 查询名字中含有「风」字的学生信息

SELECT *
FROM student
WHERE student.Sname LIKE '%风%'

3.25 查询同名学生名单,并统计同名人数

找到同名的名字并统计个数:

SELECT sname, count(*)
FROM student
GROUP BY sname
HAVING count(*) > 1;

嵌套查询列出同名的全部学生的信息:

SELECT *
FROM student
WHERE sname IN (
 SELECT sname
 FROM student
 GROUP BY sname
 HAVING count(*) > 1
);

3.26 查询 1990 年出生的学生名单

SELECT *
FROM student
WHERE YEAR(student.Sage) = 1990;

3.27 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列

SELECT sc.cid, course.cname, AVG(SC.SCORE) AS average
FROM sc, course
WHERE sc.cid = course.cid
GROUP BY sc.cid
ORDER BY average DESC, cid ASC;

3.28 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩

having 也可以用来截取结果表,在这里就先得到平均成绩总表,再截取 AVG 大于 85 的即可。

SELECT student.sid, student.sname, AVG(sc.score) AS aver
FROM student, sc
WHERE student.sid = sc.sid
GROUP BY sc.sid
HAVING aver > 85;

3.29 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数

SELECT student.sname, sc.score
FROM student, sc, course
WHERE student.sid = sc.sid
 AND course.cid = sc.cid
 AND course.cname = '数学'
 AND sc.score < 60;

3.30 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)

SELECT student.sname, cid, score
FROM student
 LEFT JOIN sc ON student.sid = sc.sid;

3.31 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数

SELECT student.sname, course.cname, sc.score
FROM student, course, sc
WHERE sc.score > 70
 AND student.sid = sc.sid
 AND sc.cid = course.cid;

3.32 查询存在不及格的课程

可以用 group by 来取唯一,也可以用 distinct。

SELECT cid
FROM sc
WHERE score < 60
GROUP BY cid;
SELECT DISTINCT sc.CId
FROM sc
WHERE sc.score < 60;

3.33 查询课程编号为 01 且课程成绩在 80 分及以上的学生的学号和姓名

SELECT student.sid, student.sname
FROM student, sc
WHERE cid = '01'
 AND score >= 80
 AND student.sid = sc.sid;

3.34 求每门课程的学生人数

SELECT sc.CId, count(*) AS 学生人数
FROM sc
GROUP BY sc.CId;

3.35 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

用 having max() 理论上也是对的,但是下面那种按分数排序然后取 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 = '张三'

HAVING max(sc.score);
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 = '张三'
ORDER BY score DESC
LIMIT 1;

3.36 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

这道题的思路继续上一题,我们已经查询到了符合限定条件的最高分了,这个时候只用比较这张表,找到全部 score 等于这个最高分的记录就可,看起来有点繁复。

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 = '张三'
 );

3.37 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

同上,在这里用了 inner join 后会有概念是重复的记录:“ 01 课与 03 课”=“ 03 课与 01课”,所以这里取唯一可以直接用 group by。

SELECT a.cid, a.sid, a.score
FROM sc a
 INNER JOIN sc b
 ON a.sid = b.sid
  AND a.cid != b.cid
  AND a.score = b.score
GROUP BY cid, sid;

3.38 查询每门功成绩最好的前两名

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.cid, a.sid
HAVING count(b.cid) < 2
ORDER BY a.cid;

3.39 统计每门课程的学生选修人数(超过 5 人的课程才统计)

SELECT sc.cid, count(sid) AS cc
FROM sc
GROUP BY cid
HAVING cc > 5;

3.40 检索至少选修两门课程的学生学号

SELECT sid, count(cid) AS cc
FROM sc
GROUP BY sid
HAVING cc >= 2;

3.41 查询选修了全部课程的学生信息

SELECT student.*
FROM sc, student
WHERE sc.SId = student.SId
GROUP BY sc.SId
HAVING count(*) = (
 SELECT DISTINCT count(*)
 FROM course
)

3.42 查询各学生的年龄,只按年份来算

一般都用 41 题的方法精确到天,按照出生日期来算,当前月日 <出生年月的月日则,年龄减一。

SELECT student.SId AS 学生编号, student.Sname AS 学生姓名
 , TIMESTAMPDIFF(YEAR, student.Sage, CURDATE()) AS 学生年龄
FROM student

3.43 查询本周过生日的学生

SELECT *
FROM student
WHERE WEEKOFYEAR(student.Sage) = WEEKOFYEAR(CURDATE());

3.44 查询下周过生日的学生

SELECT *
FROM student
WHERE WEEKOFYEAR(student.Sage) = WEEKOFYEAR(CURDATE()) + 1;

3.45 查询本月过生日的学生

SELECT *
FROM student
WHERE MONTH(student.Sage) = MONTH(CURDATE());

3.46 查询下月过生日的学生

SELECT *
FROM student
WHERE MONTH(student.Sage) = MONTH(CURDATE()) + 1;

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8