50道SQL练习题MySQL版,答案以及详解
本文最后更新于 2020年08月13日

前言

纯手打,还有几道不会,争取这几天补上,发这篇博文主要是督促自己,不要偷懒????

限于Wordpress排版,显示的不是很好看,已经尽力了

可以直接复制后导入到MySQL

/*
 Navicat Premium Data Transfer

 Source Server         : MySQL
 Source Server Type    : MySQL
 Source Server Version : 50724
 Source Host           : localhost:3306
 Source Schema         : test

 Target Server Type    : MySQL
 Target Server Version : 50724
 File Encoding         : 65001

 Date: 23/01/2019 11:46:37
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for Course
-- ----------------------------
DROP TABLE IF EXISTS `Course`;
CREATE TABLE `Course` (
  `CId` varchar(10) DEFAULT NULL,
  `Cname` varchar(10) DEFAULT NULL,
  `TId` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of Course
-- ----------------------------
BEGIN;
INSERT INTO `Course` VALUES ('01', '语文', '02');
INSERT INTO `Course` VALUES ('02', '数学', '01');
INSERT INTO `Course` VALUES ('03', '英语', '03');
COMMIT;

-- ----------------------------
-- Table structure for SC
-- ----------------------------
DROP TABLE IF EXISTS `SC`;
CREATE TABLE `SC` (
  `SId` varchar(10) DEFAULT NULL,
  `CId` varchar(10) DEFAULT NULL,
  `score` decimal(18,1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of SC
-- ----------------------------
BEGIN;
INSERT INTO `SC` VALUES ('01', '01', 80.0);
INSERT INTO `SC` VALUES ('01', '02', 90.0);
INSERT INTO `SC` VALUES ('01', '03', 99.0);
INSERT INTO `SC` VALUES ('02', '01', 70.0);
INSERT INTO `SC` VALUES ('02', '02', 60.0);
INSERT INTO `SC` VALUES ('02', '03', 80.0);
INSERT INTO `SC` VALUES ('03', '01', 80.0);
INSERT INTO `SC` VALUES ('03', '02', 80.0);
INSERT INTO `SC` VALUES ('03', '03', 80.0);
INSERT INTO `SC` VALUES ('04', '01', 50.0);
INSERT INTO `SC` VALUES ('04', '02', 30.0);
INSERT INTO `SC` VALUES ('04', '03', 20.0);
INSERT INTO `SC` VALUES ('05', '01', 76.0);
INSERT INTO `SC` VALUES ('05', '02', 87.0);
INSERT INTO `SC` VALUES ('06', '01', 31.0);
INSERT INTO `SC` VALUES ('06', '03', 34.0);
INSERT INTO `SC` VALUES ('07', '02', 89.0);
INSERT INTO `SC` VALUES ('07', '03', 98.0);
COMMIT;

-- ----------------------------
-- Table structure for Student
-- ----------------------------
DROP TABLE IF EXISTS `Student`;
CREATE TABLE `Student` (
  `SId` varchar(10) DEFAULT NULL,
  `Sname` varchar(10) DEFAULT NULL,
  `Sage` datetime DEFAULT NULL,
  `Ssex` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of Student
-- ----------------------------
BEGIN;
INSERT INTO `Student` VALUES ('01', '赵雷', '1990-01-01 00:00:00', '男');
INSERT INTO `Student` VALUES ('02', '钱电', '1990-12-21 00:00:00', '男');
INSERT INTO `Student` VALUES ('03', '孙风', '1990-12-20 00:00:00', '男');
INSERT INTO `Student` VALUES ('04', '李云', '1990-12-06 00:00:00', '男');
INSERT INTO `Student` VALUES ('05', '周梅', '1991-12-01 00:00:00', '女');
INSERT INTO `Student` VALUES ('06', '吴兰', '1992-01-01 00:00:00', '女');
INSERT INTO `Student` VALUES ('07', '郑竹', '1989-01-01 00:00:00', '女');
INSERT INTO `Student` VALUES ('09', '张三', '2017-12-20 00:00:00', '女');
INSERT INTO `Student` VALUES ('10', '李四', '2017-12-25 00:00:00', '女');
INSERT INTO `Student` VALUES ('11', '李四', '2012-06-06 00:00:00', '女');
INSERT INTO `Student` VALUES ('12', '赵六', '2013-06-13 00:00:00', '女');
INSERT INTO `Student` VALUES ('13', '孙七', '2014-06-01 00:00:00', '女');
COMMIT;

-- ----------------------------
-- Table structure for Teacher
-- ----------------------------
DROP TABLE IF EXISTS `Teacher`;
CREATE TABLE `Teacher` (
  `TId` varchar(10) DEFAULT NULL,
  `Tname` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of Teacher
-- ----------------------------
BEGIN;
INSERT INTO `Teacher` VALUES ('01', '张三');
INSERT INTO `Teacher` VALUES ('02', '李四');
INSERT INTO `Teacher` VALUES ('03', '王五');
COMMIT;

SET FOREIGN_KEY_CHECKS = 1;

表内容

1.学生表

Student(SId,Sname,Sage,Ssex)

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

2.课程表

Course(CId,Cname,TId)

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

3.教师表

Teacher(TId,Tname)

TId 教师编号,Tname 教师姓名

4.成绩表

SC(SId,CId,score)

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

题目

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

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

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

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

2. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩

3. 查询在 SC 表存在成绩的学生信息

4. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )

4.1 查有成绩的学生信息

5. 查询「李」姓老师的数量

6. 查询学过「张三」老师授课的同学的信息

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

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

9. 查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息

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

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

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

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

14. 查询各科成绩最高分、最低分和平均分:

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

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

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

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

15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次

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

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

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

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

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

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

21. 查询男生、女生人数

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

23. 查询同名同性学生名单,并统计同名人数

24. 查询 1990 年出生的学生名单

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

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

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

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

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

30. 查询不及格的课程

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

32. 求每门课程的学生人数

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

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

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

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

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

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

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

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

41. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一

42. 查询本周过生日的学生

43. 查询下周过生日的学生

44. 查询本月过生日的学生

45. 查询下月过生日的学生

答案以及详解(详解我会之后更新)

–第一题答案

SELECT * FROM 
(SELECT SId,score FROM SC WHERE CId='01')  t1,
(SELECT SId,score FROM SC WHERE CId='02')  t2
WHERE t1.SId=t2.SId
AND t1.score>t2.score

–第二题答案

SELECT Student.*,t1.scavg FROM Student INNER JOIN
(SELECT SC.SId,AVG(SC.score)AS scavg FROM SC GROUP BY SC.SId
HAVING AVG(SC.score)>=60)AS t1
ON Student.SId=t1.SId

–第三题答案

SELECT DISTINCT Student.*
FROM Student,SC
WHERE Student.SId=SC.SId

–第四题答案

SELECT Student.*,t1.countscid AS 选课数目,t1.sumcore AS 总成绩 FROM Student,
(SELECT SC.SId,SUM(SC.score)AS sumcore,COUNT(SC.CId)AS countscid FROM SC GROUP BY SC.SId) t1
WHERE Student.SId=t1.SId ORDER BY t1.sumcore DESC

–第五题答案

SELECT COUNT(*) FROM Teacher WHERE Teacher.Tname LIKE '李%'

–第六题答案

SELECT Teacher.Tname,Student.*,Course.Cname
FROM Teacher,Course,Student,SC
WHERE Teacher.Tname='张三'
AND Course.TId=Teacher.TId
AND SC.CId=Course.CId
AND SC.SId=Student.SId

–第七题答案

SELECT Student.*,t1.cnum FROM Student,
(SELECT SC.SId,COUNT(SC.CId)AS cnum FROM SC GROUP BY SC.SId) t1
WHERE Student.SId=t1.SId AND t1.cnum<3

–第八题答案

SELECT DISTINCT Student.*
FROM SC,Student
WHERE SC.CId IN (SELECT SC.CId FROM SC WHERE SC.SId='01')

–第九题答案(不会)

–第十题答案

SELECT *
FROM Student
WHERE Student.SId NOT IN(
SELECT SC.SId FROM SC WHERE SC.CId IN(
SELECT Course.CId FROM Course,Teacher
WHERE Teacher.Tname='张三'
AND Teacher.TId=Course.TId))

–第十一题答案

SELECT Student.*,t1.scavg
FROM Student
RIGHT JOIN
(SELECT SC.SId,AVG(SC.score)AS scavg
FROM Student,SC
WHERE Student.SId=SC.SId
AND SC.score<60
GROUP BY SC.SId
HAVING COUNT(*)>=2) t1
ON Student.SId=t1.SId

–第十二题答案

SELECT Student.*,SC.score 
FROM Student,SC
WHERE Student.SId=SC.SId
AND SC.CId='01'
AND SC.score<60
ORDER BY SC.score DESC

–第十三题答案

SELECT Student.*,t0.scavg AS 平均分,t1.score AS 语文,t2.score AS 数学,t3.score AS 英语
FROM Student
LEFT JOIN (SELECT SC.SId,AVG(SC.score)AS scavg
FROM SC
GROUP BY SC.SId) t0
ON Student.SId=t0.SId
LEFT JOIN (SELECT SC.SId,SC.score
FROM SC
WHERE SC.CId='01') t1
ON Student.SId=t1.SId
LEFT JOIN (SELECT SC.SId,SC.score
FROM SC
WHERE SC.CId='02') t2
ON Student.SId=t2.SId
LEFT JOIN (SELECT SC.SId,SC.score
FROM SC
WHERE SC.CId='03') t3
ON Student.SId=t3.SId
ORDER BY t0.scavg DESC

–第十四题答案

SELECT SC.CId,MAX(SC.score),MIN(SC.score),AVG(SC.score)
FROM SC
GROUP BY SC.CId
ORDER BY SC.CId

–第十五题答案(还没做有点难)

–第十九题答案

SELECT Course.Cname,t1.num
FROM Course
LEFT JOIN (SELECT SC.CId,COUNT(*)AS num
FROM SC
GROUP BY SC.CId)AS t1
ON t1.CId=Course.CId

–第二十题答案

SELECT Student.SId,Student.Sname
FROM SC,Student
WHERE Student.SId=SC.SId
GROUP BY SC.SId,Student.Sname
HAVING COUNT(*)>2

–第二一题答案

SELECT Student.Ssex,COUNT(Student.SId)
FROM Student
GROUP BY Student.Ssex

–第二二题答案

SELECT Student.*
FROM Student
WHERE Student.Sname LIKE "%风"

–第二三题答案

SELECT *
FROM Student
LEFT JOIN (SELECT Student.Sname,Student.Ssex,COUNT(*)AS num
FROM Student
GROUP BY Student.Sname,Student.Ssex)AS t1
ON Student.Sname=t1.Sname
AND Student.Ssex=t1.Ssex
WHERE t1.num>1

–第二四题答案

SELECT *
FROM Student
WHERE YEAR(Student.Sage)=1990

–第二五题答案

SELECT SC.CId,AVG(SC.score)
FROM SC
GROUP BY SC.CId
ORDER BY AVG(SC.score) DESC,SC.CId ASC

–第二六题答案

SELECT Student.SId,Student.Sname,t1.scavg
FROM Student
LEFT JOIN (SELECT SC.SId,AVG(SC.score)AS scavg
FROM SC
GROUP BY SC.SId)AS t1
ON Student.SId=t1.SId
WHERE t1.scavg>85

–第二七题答案

SELECT Course.Cname,Student.Sname,SC.score
FROM SC,Course,Student
WHERE SC.score<60
AND SC.CId=02
AND SC.CId=Course.CId
AND Student.SId=SC.SId

–第二八题答案

SELECT Student.*,SC.CId,Course.Cname,SC.score
FROM Student
LEFT JOIN SC
ON Student.SId=SC.SId
LEFT JOIN Course
ON Course.CId=SC.CId

–第二九题答案

SELECT Student.Sname,Course.Cname,SC.score
FROM Student,SC,Course
WHERE Student.SId=SC.SId
AND SC.score>70
AND SC.CId=Course.CId

–第三十题答案

SELECT DISTINCT SC.CId,Course.Cname
FROM SC,Course
WHERE SC.score<60
AND SC.CId=Course.CId

–第三十一题答案

SELECT Student.SId,Student.Sname
FROM Student,SC
WHERE SC.CId='01'
AND SC.SId=Student.SId
AND	SC.score>80

–第三十二题答案

SELECT Course.Cname,COUNT(SC.SId)
FROM Course,SC
WHERE SC.CId=Course.CId
GROUP BY Course.Cname

–第三十三题答案

SELECT Student.*,SC.score
FROM Student,Teacher,Course,SC
WHERE Teacher.Tname='张三'
AND Course.CId=SC.CId
AND Teacher.TId=Course.TId
AND Student.SId=SC.SId
ORDER BY SC.score DESC LIMIT 1

–第三十四题答案


–第三十五题答案

SELECT *
FROM SC AS t1
WHERE EXISTS (SELECT *
FROM SC AS t2
WHERE t1.CId!=t2.CId
AND t1.score=t2.score
AND t1.SId!=t2.SId)

–第三十六题答案

SELECT *
FROM SC AS t1
WHERE (SELECT COUNT(*)
FROM SC AS t2
WHERE t1.CId=t2.CId
AND t2.score>t1.score)<2
ORDER BY t1.CId 

–第三十七题答案

SELECT SC.CId,COUNT(*)
FROM SC
GROUP BY SC.CId
HAVING COUNT(*)>5

–第三十八题答案

SELECT DISTINCT t1.SId
FROM SC AS t1
WHERE (SELECT COUNT(*)
FROM SC
WHERE SC.SId=t1.SId
GROUP BY SC.SId)>=3

–第三十九题答案

SELECT Student.*
FROM Student
WHERE (SELECT COUNT(*)
FROM SC
WHERE SC.SId=Student.SId
GROUP BY SC.SId)=3

–第四十题答案

SELECT Student.*,TIMESTAMPDIFF(YEAR,Student.Sage,CURDATE())AS age
FROM Student

–第四十一题答案


–第四十二题答案

SELECT *
FROM Student
WHERE YEARWEEK(Student.Sage)=YEAR(CURDATE())

–第四十三题答案

SELECT *
FROM Student
WHERE YEARWEEK(Student.Sage)=CONCAT(YEAR(CURDATE()),WEEK(CURDATE())+1)

–第四十三题答案

SELECT *
FROM Student
WHERE YEARWEEK(Student.Sage)=CONCAT(YEAR(CURDATE()),WEEK(CURDATE())+1)

–第四十四题答案

SELECT *
FROM Student
WHERE EXTRACT(YEAR_MONTH FROM Student.Sage)=EXTRACT(YEAR_MONTH FROM CURDATE())

–第四十五题答案

SELECT *
FROM Student
WHERE EXTRACT(YEAR_MONTH FROM Student.Sage)=EXTRACT(YEAR_MONTH FROM DATE_ADD(CURDATE(),INTERVAL 1 MONTH) )
原创声明
本文由 NG6 于2019年01月23日发表在 sleele的博客
如未特殊声明,本站所有文章均为原创;你可以在保留作者及原文地址的情况下转载
转载请注明:50道SQL练习题MySQL版,答案以及详解 | sleele的博客

评论

  1. 匿名
    Windows Chrome
    5年前
    2019-12-26 18:38:51

    第九题,这样做看看:
    select
    SId,count(SId) as num
    from qikegu_demo.sc
    where CId in (select CId from qikegu_demo.sc where SId = ’01’)
    group by SId
    having num = (select count(CId) from qikegu_demo.sc where SId = ’01’);

    • NG6
      匿名
      博主
      Jetpack
      5年前
      2019-12-28 11:51:30

      @匿名 感谢回复,一直放着拖着,都快给忘了

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇