目录
一、前言
SQL是数据库开发的核心语言,也是软件开发中必备的基础技能。本文精选了50道SQL查询练习题,旨在帮助初学者和从业人员夯实和拓展SQL编程能力。前面题目解题过程会略有冗余,会将解题思路一步一步展现,为了帮助初学者降低学习门槛。后面随着题目增多,会简洁高效直接列出答案。
二、50道题目
查询" 01 “课程比” 02 "课程成绩高的学生的信息及课程分数
查询同时存在" 01 “课程和” 02 "课程的情况
查询存在" 01 “课程但可能不存在” 02 "课程的情况(不存在时显示为 null )
查询不存在" 01 “课程但存在” 02 "课程的情况
查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
查有成绩的学生信息
查询「李」姓老师的数量
查询学过「张三」老师授课的同学的信息
查询没有学全所有课程的同学的信息
查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息
查询没学过"张三"老师讲授的任一门课程的学生姓名
查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
检索" 01 "课程分数小于 60,按分数降序排列的学生信息
按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
查询各科成绩最高分、最低分和平均分: 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺
按各科成绩进行排序,并显示排名, Score 重复时合并名次
查询学生的总成绩,并进行排名,总分重复时保留名次空缺
查询学生的总成绩,并进行排名,总分重复时不保留名次空缺
统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占人数
查询各科成绩前三名的记录
查询每门课程被选修的学生数
查询出只选修两门课程的学生学号和姓名
查询男生、女生人数
查询名字中含有「风」字的学生信息
查询同名同性学生名单,并统计同名人数
查询 1990 年出生的学生名单
查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
查询不及格的课程
查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名
求每门课程的学生人数
成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
查询每门功成绩最好的前两名
统计每门课程的学生选修人数(超过 5 人的课程才统计)。
检索至少选修两门课程的学生学号
查询选修了全部课程的学生信息
查询各学生的年龄,只按年份来算
按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
查询本周过生日的学生
查询下周过生日的学生
查询本月过生日的学生
查询下月过生日的学生
三、数据库表
CREATE TABLE `Course` (
`cid` varchar(10) CHARACTER SET latin1 NOT NULL COMMENT '课程编号',
`cname` varchar(10) DEFAULT NULL COMMENT '课程名称',
`tid` varchar(10) CHARACTER SET latin1 DEFAULT NULL COMMENT '老师编号',
PRIMARY KEY (`cid`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='课程表';
INSERT INTO `Course`(`cid`, `cname`, `tid`) VALUES ('01', '语文', '02');
INSERT INTO `Course`(`cid`, `cname`, `tid`) VALUES ('02', '数学', '01');
INSERT INTO `Course`(`cid`, `cname`, `tid`) VALUES ('03', '英语', '03');
CREATE TABLE `SC` (
`sid` varchar(10) DEFAULT NULL COMMENT '学生编号',
`cid` varchar(10) DEFAULT NULL COMMENT '课程编号',
`score` decimal(18,1) DEFAULT NULL COMMENT '课程分数'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='成绩表';
INSERT INTO `SC`(`sid`, `cid`, `score`) VALUES ('01', '01', 80.0);
INSERT INTO `SC`(`sid`, `cid`, `score`) VALUES ('01', '02', 90.0);
INSERT INTO `SC`(`sid`, `cid`, `score`) VALUES ('01', '03', 99.0);
INSERT INTO `SC`(`sid`, `cid`, `score`) VALUES ('02', '01', 70.0);
INSERT INTO `SC`(`sid`, `cid`, `score`) VALUES ('02', '02', 60.0);
INSERT INTO `SC`(`sid`, `cid`, `score`) VALUES ('02', '03', 80.0);
INSERT INTO `SC`(`sid`, `cid`, `score`) VALUES ('03', '01', 80.0);
INSERT INTO `SC`(`sid`, `cid`, `score`) VALUES ('03', '02', 80.0);
INSERT INTO `SC`(`sid`, `cid`, `score`) VALUES ('03', '03', 80.0);
INSERT INTO `SC`(`sid`, `cid`, `score`) VALUES ('04', '01', 50.0);
INSERT INTO `SC`(`sid`, `cid`, `score`) VALUES ('04', '02', 30.0);
INSERT INTO `SC`(`sid`, `cid`, `score`) VALUES ('04', '03', 20.0);
INSERT INTO `SC`(`sid`, `cid`, `score`) VALUES ('05', '01', 76.0);
INSERT INTO `SC`(`sid`, `cid`, `score`) VALUES ('05', '02', 87.0);
INSERT INTO `SC`(`sid`, `cid`, `score`) VALUES ('06', '01', 31.0);
INSERT INTO `SC`(`sid`, `cid`, `score`) VALUES ('06', '03', 34.0);
INSERT INTO `SC`(`sid`, `cid`, `score`) VALUES ('07', '02', 89.0);
INSERT INTO `SC`(`sid`, `cid`, `score`) VALUES ('07', '03', 98.0);
CREATE TABLE `Student` (
`sid` varchar(10) NOT NULL COMMENT '学生编号',
`sname` varchar(10) DEFAULT NULL COMMENT '学生姓名',
`sage` datetime DEFAULT NULL COMMENT '学生年龄',
`ssex` varchar(10) DEFAULT NULL COMMENT '学生性别',
PRIMARY KEY (`sid`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='学生表';
INSERT INTO `Student`(`sid`, `sname`, `sage`, `ssex`) VALUES ('01', '赵雷', '1990-01-01', '男');
INSERT INTO `Student`(`sid`, `sname`, `sage`, `ssex`) VALUES ('02', '钱电', '1990-12-21', '男');
INSERT INTO `Student`(`sid`, `sname`, `sage`, `ssex`) VALUES ('03', '孙风', '1990-12-20', '男');
INSERT INTO `Student`(`sid`, `sname`, `sage`, `ssex`) VALUES ('04', '李云', '1990-12-06', '男');
INSERT INTO `Student`(`sid`, `sname`, `sage`, `ssex`) VALUES ('05', '周梅', '1991-12-01', '女');
INSERT INTO `Student`(`sid`, `sname`, `sage`, `ssex`) VALUES ('06', '吴兰', '1992-01-01', '女');
INSERT INTO `Student`(`sid`, `sname`, `sage`, `ssex`) VALUES ('07', '郑竹', '1989-01-01', '女');
INSERT INTO `Student`(`sid`, `sname`, `sage`, `ssex`) VALUES ('09', '张三', '2017-12-20', '女');
INSERT INTO `Student`(`sid`, `sname`, `sage`, `ssex`) VALUES ('10', '李四', '2017-12-25', '女');
INSERT INTO `Student`(`sid`, `sname`, `sage`, `ssex`) VALUES ('11', '李四', '2012-06-06', '女');
INSERT INTO `Student`(`sid`, `sname`, `sage`, `ssex`) VALUES ('12', '赵六', '2013-06-13', '女');
INSERT INTO `Student`(`sid`, `sname`, `sage`, `ssex`) VALUES ('13', '孙七', '2014-06-01', '女');
CREATE TABLE `Teacher` (
`tid` varchar(10) CHARACTER SET utf8 NOT NULL COMMENT '教师编号',
`tname` varchar(10) CHARACTER SET utf8 DEFAULT NULL COMMENT '教师姓名',
PRIMARY KEY (`tid`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='教师表';
INSERT INTO `Teacher`(`tid`, `tname`) VALUES ('01', '张三');
INSERT INTO `Teacher`(`tid`, `tname`) VALUES ('02', '李四');
INSERT INTO `Teacher`(`tid`, `tname`) VALUES ('03', '王五');
四、50道题目详解
01-05题详解
1.查询"01"课程比"02"课程成绩高的学生的信息及课程分数.
SELECT Student.*,t3.score01,t3.score02 FROM Student RIGHT JOIN (
SELECT t1.sid,t1.score01,t2.score02 FROM
(SELECT sid,score AS score01 FROM SC WHERE Cid='01') AS t1,
(SELECT sid,score AS score02 FROM SC WHERE Cid='02') AS t2
WHERE t1.sid=t2.sid and t1.score01>t2.score02
) As t3 ON t3.sid=Student.sid;
-- 1. 查询有01课程分数学生。
SELECT sid,score FROM SC WHERE Cid='01';
-- 2. 查询有02课程分数学生。
SELECT sid,score FROM SC WHERE Cid='02';
-- 3. 查询"01"课程比"02"课程成绩高的学生编号及课程分数
SELECT t1.sid,t1.score01,t2.score02 FROM
(SELECT sid,score AS score01 FROM SC WHERE Cid='01') AS t1,
(SELECT sid,score AS score02 FROM SC WHERE Cid='02') AS t2
WHERE t1.sid=t2.sid and t1.score01>t2.score02;
-- 4.查询"01"课程比"02"课程成绩高的学生的信息及课程分数。
SELECT Student.*,t3.score01,t3.score02 FROM Student RIGHT JOIN (
SELECT t1.sid,t1.score01,t2.score02 FROM
(SELECT sid,score AS score01 FROM SC WHERE Cid='01') AS t1,
(SELECT sid,score AS score02 FROM SC WHERE Cid='02') AS t2
WHERE t1.sid=t2.sid and t1.score01>t2.score02
) As t3 ON t3.sid=Student.sid;
2.查询同时存在" 01"课程和"02"课程的情况.
SELECT t1.sid,t1.score01,t2.score02 FROM
(SELECT sid,score AS score01 FROM SC WHERE Cid='01') AS t1,
(SELECT sid,score AS score02 FROM SC WHERE Cid='02') AS t2
WHERE t1.sid=t2.sid ;
3.查询存在"01"课程但可能不存在"02"课程的情况(不存在时显示为 null)
SELECT t1.sid,t1.score01,t2.score02 FROM
(SELECT sid,score AS score01 FROM SC WHERE Cid='01' )AS t1
LEFT JOIN (SELECT sid,score AS score02 FROM SC WHERE Cid='02') AS t2
ON t1.sid=t2.sid;
4.查询不存在" 01"课程但存在" 02"课程的情况
-- 1.查询有02课程分数学生。
SELECT sid,score AS score02 FROM SC WHERE Cid='02';
-- 2.查询有01课程分数学生编号。
SELECT sid FROM SC WHERE Cid='01';
-- 3.排除有01课程分数学生。
SELECT * FROM
(SELECT sid,score AS score02 FROM SC WHERE Cid='02') AS t2
WHERE sid NOT IN (SELECT sid FROM SC WHERE Cid='01');
-- 这里也可以用"EXISTS"
SELECT * FROM
(SELECT sid,score AS score02 FROM SC WHERE Cid='02') AS t2
WHERE Not EXISTS (SELECT sid FROM SC WHERE Cid='01' and t2.sid=SC.sid);
5.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
SELECT Student.*, t1.avgscore FROM Student,
(SELECT sid, AVG(score) AS avgscore FROM SC GROUP BY sid HAVING avgscore> 60) AS t1
WHERE t1.sid=Student.sid;
-- 这里也可以用RIGHT JOIN(以右侧平均分数表为准)
SELECT Student.*, t1.avgscore FROM Student RIGHT JOIN
(SELECT sid, AVG(score) AS avgscore FROM SC GROUP BY sid HAVING avgscore> 60) AS t1
on t1.sid=Student.sid;
06-10题详解
6.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为null)
SELECT Student.*,t1.countcid,t1.sumscore FROM Student LEFT JOIN
(SELECT sid,count(cid) As countcid,sum(score) As sumscore FROM SC GROUP BY sid) as t1
ON Student.sid=t1.sid;
7.查有成绩的学生信息
-- DISTINCT 返回唯一条记录
SELECT * FROM Student WHERE sid in (SELECT DISTINCT sid FROM SC);
-- 也可以这么写
SELECT * FROM Student WHERE sid in (SELECT sid FROM SC);
-- 还可以这样写
SELECT * FROM Student WHERE EXISTS (SELECT sid FROM SC WHERE Student.sid=sc.sid);
8.查询「李」姓老师的数量
SELECT count(*) FROM Teacher WHERE Tname Like '%李%';
-- 1.以'李'开头
SELECT * FROM Teacher WHERE tname Like '李%';
-- 2.以'李'结尾
SELECT * FROM Teacher WHERE tname Like '%李';
-- 3.包含'李'
SELECT * FROM Teacher WHERE tname Like '%李%';
-- 4.不包含'李'
SELECT * FROM Teacher WHERE tname NOT Like '%李%';
9.查询学过「张三」老师授课的同学的信息
SELECT * FROM Student WHERE sid IN
(SELECT sid FROM SC WHERE cid in
(SELECT cid FROM Course WHERE tid in
(SELECT tid FROM Teacher WHERE tname='张三')));
-- 1.查下张三老师的课程编码
SELECT cid FROM Course WHERE tid in
(SELECT tid FROM Teacher WHERE tname='张三');
-- 2.查下上过张三老师课的学生编码
SELECT sid FROM SC WHERE cid in
(SELECT cid FROM Course WHERE tid in
(SELECT tid FROM Teacher WHERE tname='张三'));
-- 3.通过学生编码查询下学生信息
SELECT * FROM Student WHERE sid IN
(SELECT sid FROM SC WHERE cid in
(SELECT cid FROM Course WHERE tid in
(SELECT tid FROM Teacher WHERE tname='张三')));
-- 也可以联表查询
SELECT Student.* FROM Student,Teacher,Course,SC
WHERE Student.sid=SC.sid
and SC.cid=Course.cid
and Course.tid=Teacher.tid
and Teacher.tname='张三';
10.查询没有学全所有课程的同学的信息
-- 1.查询一共有几门课程
SELECT count(cid) FROM Course;
-- 2.查询成绩表中分数记录等于课程总数的学生。
SELECT sid,count(cid) FROM SC
GROUP BY sid HAVING count(cid) =(SELECT count(cid) FROM Course);
-- 3.将学全所有课程的学生排除
SELECT * FROM Student
WHERE sid not IN(SELECT sid FROM SC GROUP BY sid
HAVING count(cid) =(SELECT count(cid) FROM Course));
11-15题详解
11.查询至少有一门课与学号为"01"的同学所学相同的同学的信息
-- 1.查询01同学所学的课程
SELECT cid FROM SC WHERE sid='01';
-- 2.查询至少有一门课与01同学相同的同学
SELECT DISTINCT sid FROM SC WHERE cid IN (SELECT cid FROM SC WHERE sid='01');
-- 3.查询下学生的信息
SELECT * FROM Student WHERE sid
IN(SELECT DISTINCT sid FROM SC WHERE cid IN (SELECT cid FROM SC WHERE sid='01'));
12.查询和"01"号的同学学习的课程完全相同的其他同学的信息
SELECT Student.*,t1.concatcid FROM Student,
(SELECT sid,group_concat(cid ORDER BY cid) as concatcid FROM SC GROUP BY sid) as t1,
(SELECT group_concat(cid ORDER BY cid) as concatcid FROM SC WHERE sid='01') as t2
WHERE t1.sid=Student.sid and t1.concatcid=t2.concatcid
-- 1.查询01同学所学的课程
SELECT cid FROM SC WHERE sid='01';
-- 2.查询01同学所学的课程 group_concat。“(cid ORDER BY cid)” 进行排序。
--(避免01,02,03和01,03,02等类似情况)
SELECT group_concat(cid ORDER BY cid) as concatcid FROM SC WHERE sid='01'.;
-- 3.所学的课程并且 group_concat
SELECT sid,group_concat(cid ORDER BY cid) as concatcid FROM SC GROUP BY sid;
-- 4.对比concatcid是否相同得到结果
SELECT Student.*,t1.concatcid FROM Student,
(SELECT sid,group_concat(cid ORDER BY cid) as concatcid FROM SC GROUP BY sid) as t1,
(SELECT group_concat(cid ORDER BY cid) as concatcid FROM SC WHERE sid='01') as t2
WHERE t1.sid=Student.sid and t1.concatcid=t2.concatcid
13.查询没学过"张三"老师讲授的任一门课程的学生姓名
SELECT * FROM Student WHERE sid NOT IN (
SELECT DISTINCT sid FROM sc WHERE cid IN (
SELECT cid FROM Course WHERE tid=(
SELECT tid FROM Teacher WHERE tname='张三'))
)
-- 1.查看张三老师的课程
SELECT cid FROM Course WHERE tid=(SELECT tid FROM Teacher WHERE tname='张三') ;
-- 2.查看上过张三老师的课程的学生id
SELECT DISTINCT sid FROM sc WHERE cid in
(SELECT cid FROM Course WHERE tid=(SELECT tid FROM Teacher WHERE tname='张三'));
-- 3.查询没有上过张三老师课程的学生信息
SELECT * FROM Student WHERE sid NOT IN (
SELECT DISTINCT sid FROM sc WHERE cid IN (
SELECT cid FROM Course WHERE tid=(
SELECT tid FROM Teacher WHERE tname='张三'))
)
14.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECT Student.sid,Student.sname,AVG(SC.score) FROM Student,SC
WHERE Student.sid=SC.sid
and SC.score< 60
GROUP BY Student.sid
HAVING count(*) > 1
-- 1.查询不及格同学
SELECT sid,score FROM sc WHERE score< 60;
-- 2.统计不及格同学的课程数
SELECT sid,count(score) FROM sc WHERE score <60 GROUP BY sid;
-- 3.查询两门及其以上不及格课程的同学的学号
SELECT sid,count(score) FROM sc WHERE score <60 GROUP BY sid HAVING count(score)>=2;
-- 4. 算平均成绩
SELECT Student.sid,Student.sname,AVG(SC.score) FROM Student,SC,
(SELECT sid FROM sc WHERE score <60 GROUP BY sid HAVING count(score)>=2) AS t1
WHERE Student.sid=t1.sid
and t1.sid=sc.sid
GROUP BY Student.sid
-- 可以直接联表查询
SELECT Student.sid,Student.sname,AVG(SC.score) FROM Student,SC
WHERE Student.sid=SC.sid
and SC.score< 60
GROUP BY Student.sid
HAVING count(*) > 1
15.检索" 01 "课程分数小于 60,按分数降序排列的学生信息
SELECT Student.*,SC.score FROM Student,SC
WHERE cid='01'
and score<60
and Student.sid=sc.sid
ORDER BY score DESC;
16-20题详解
16.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
SELECT * FROM SC LEFT JOIN (SELECT sid, AVG(score) as avgscore FROM SC
GROUP BY sid ORDER BY avgscore DESC) r
ON SC.sid = r.sid ORDER BY r.avgscore DESC;
17.查询各科成绩最高分、最低分和平均分: 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列。
------最高分,最低分,平均分 这几个比较简单直接用函数。 及格率,中等率,优良率,优秀率 需要用 WHEN THEN ELSE END.例如:及格率=及格人数/选修人数。及格人数:当分数>=60记为1,否则记为0,最后用SUM累加。
SELECT Course.cname,t1.* FROM Course LEFT JOIN
(SELECT
cid,
MAX( score ) AS 最高分,
MIN( score ) AS 最低分,
AVG( score ) AS 平均分,
SUM(CASE WHEN score>=60 THEN 1 ELSE 0 END ) / COUNT(*) AS 及格率,
SUM(CASE WHEN score>=70 AND score<80 THEN 1 ELSE 0 END ) / COUNT(*) AS 中等率,
SUM(CASE WHEN score>=80 AND score<90 THEN 1 ELSE 0 END ) / COUNT(*) AS 优良率,
SUM(CASE WHEN score>=90 THEN 1 ELSE 0 END ) / COUNT(*) AS 优秀率,
COUNT(*) AS 选修人数
FROM
SC
GROUP BY SC.cid
ORDER BY COUNT(*) DESC, SC.cid ASC ) t1
ON t1.cid = Course.cid;
18.按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺
SELECT cid , sid, score , RANK() OVER (PARTITION BY cid ORDER BY score DESC) AS 排名
FROM SC;
19.按各科成绩进行排序,并显示排名, Score 重复时合并名次
SELECT cid , sid, score , DENSE_RANK() OVER (PARTITION BY cid ORDER BY score DESC) AS 排名
FROM SC;
20.查询学生的总成绩,并进行排名,总分重复时保留名次空缺
SELECT b.sid AS 学生id, b.total AS 总分, RANK() OVER (ORDER BY b.total DESC) AS 排名
FROM (SELECT sid, SUM(score) total
FROM SC
GROUP BY sid) b;
这里用到了MySQL序号函数dense_rank,序号函数详解
21-25题详解
21.查询学生的总成绩,并进行排名,总分重复时不保留名次空缺
SELECT b.sid AS 学生id, b.total AS 总分, DENSE_RANK() OVER (ORDER BY b.total DESC) AS 排名
FROM (SELECT sid, SUM(score) total
FROM SC
GROUP BY sid) b;
22.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占人数
SELECT SC.cid,Course.cname,SC.score ,
SUM(CASE WHEN SC.score<=100 and 85<SC.score THEN 1 ELSE 0 END) AS "[100-85]",
SUM(CASE WHEN SC.score<=85 and 70<SC.score THEN 1 ELSE 0 END) AS "[85-70]",
SUM(CASE WHEN SC.score<=70 and 60<SC.score THEN 1 ELSE 0 END) AS "[70-60]",
SUM(CASE WHEN SC.score<=60 and 0<SC.score THEN 1 ELSE 0 END) AS "[60-0]"
FROM SC
LEFT JOIN Course ON SC.cid = Course.cid
GROUP BY SC.cid;
23.查询各科成绩前三名的记录
SELECT * FROM
(SELECT cid,sid,score, row_number () OVER ( PARTITION BY cid ORDER BY score DESC ) AS ranks
FROM
SC) b
WHERE b.ranks<=3;
24.查询每门课程被选修的学生数
SELECT cid , count(sid) AS 选修学生数
FROM SC
GROUP BY cid;
25.查询出只选修两门课程的学生学号和姓名
SELECT SC.sid ,S.sname, count(cid) AS count_cid
FROM SC
LEFT JOIN Student S ON SC.sid = S.sid
GROUP BY sid
HAVING count_cid=2;
26-30题详解
26.查询男生、女生人数
SELECT ssex as 性别,count(ssex) as 人数
FROM Student
GROUP BY ssex;
27.查询名字中含有「风」字的学生信息
SELECT * FROM Student WHERE sname LIKE '%风%';
28.查询同名同性学生名单,并统计同名人数
SELECT sname,count(sname) as 同名人数 FROM Student
GROUP BY sname
HAVING count(sname)>1;
29.查询 1990 年出生的学生名单
SELECT * FROM Student WHERE YEAR(sage) =1990;
30.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
SELECT cid,avg(score) avg_score FROM SC GROUP BY cid ORDER BY avg_score DESC, cid ASC;
31-35题详解
31.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
SELECT SC.sid,S.sname,avg(score) avg_score FROM SC
LEFT JOIN Student S ON SC.sid = S.sid
GROUP BY SC.sid
HAVING avg_score>85;
#32.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
SELECT SC.sid,S.sname,SC.score FROM SC
LEFT JOIN Student S ON SC.sid = S.sid
WHERE SC.cid=(SELECT cid FROM Course WHERE cname='数学')
AND SC.score<60;
33.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
SELECT S.sname,SC.cid,C.cname,SC.score FROM Student S
LEFT JOIN SC ON S.sid = SC.sid
LEFT JOIN Course C ON SC.cid = C.cid;
34.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
SELECT S.sname,SC.cid,C.cname,SC.score FROM Student S
LEFT JOIN SC ON S.sid = SC.sid
LEFT JOIN Course C ON SC.cid = C.cid
WHERE SC.score>70;
35.查询不及格的课程
SELECT SC.cid,C.cname FROM SC
LEFT JOIN Course C ON SC.cid = C.cid
WHERE SC.score<60
GROUP BY SC.cid;
36-40题详解
36.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名
SELECT SC.sid,S.sname,SC.score FROM SC
LEFT JOIN Student S ON SC.sid = S.sid
WHERE SC.sid='01' AND SC.score >80;
37.求每门课程的学生人数
SELECT cid AS 课程ID,COUNT(cid) AS 学生人数 FROM SC
GROUP BY cid;
38.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
SELECT SC.cid,SC.sid,S.sname,SC.score FROM SC
LEFT JOIN Student S ON SC.sid = S.sid
WHERE SC.cid=(SELECT cid FROM Course WHERE tid=(SELECT tid FROM Teacher WHERE tname='张三'))
ORDER BY SC.score DESC
LIMIT 1;
39.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
#先查出张三老师教课程ID
SET @tempCid=(SELECT cid FROM Course WHERE tid=(SELECT tid FROM Teacher WHERE tname='张三'));
#查出最张三老师这门课程最高分
SET @max_score=(SELECT MAX(score) FROM SC WHERE cid=@tempCid) ;
#查数据
SELECT SC.cid,SC.sid,S.sname,SC.score FROM SC
LEFT JOIN Student S ON SC.sid = S.sid
WHERE SC.cid=@tempCid AND SC.score=@max_score;
40.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
SELECT sc1.sid,sc1.cid,sc1.score FROM SC sc1
JOIN SC sc2 ON sc1.sid=sc2.sid AND sc1.cid!=sc2.cid AND sc1.score=sc2.score
GROUP BY sc1.cid;
41-45题详解
41.查询每门功成绩最好的前两名
SELECT cid, sid, score, score_rank
FROM (SELECT cid, sid, score, ROW_NUMBER() OVER (PARTITION BY cid ORDER BY score DESC) AS score_rank
FROM SC) ranked_scores
WHERE score_rank <= 2;
42.统计每门课程的学生选修人数(超过 5 人的课程才统计)
SELECT cid,COUNT(sid) as 人数 FROM SC
GROUP BY cid
HAVING COUNT(sid)>5;
43.检索至少选修两门课程的学生学号
SELECT sid,COUNT(cid) as 选课数 FROM SC
GROUP BY sid
HAVING COUNT(cid)>2;
44.查询选修了全部课程的学生信息
SELECT S.sid,S.sname,COUNT(cid) as 选课数 FROM SC
LEFT JOIN Student S ON SC.sid = S.sid
GROUP BY sid
HAVING COUNT(cid)=(SELECT COUNT(*) FROM Course);
45.查询各学生的年龄,只按年份来算
SELECT sid, sname, YEAR(CURRENT_DATE)-YEAR(sage) as 年龄 FROM Student;
46-50题详解
46.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
SELECT sid,sname,sage,NOW(),TIMESTAMPDIFF(YEAR, sage, NOW()),
CASE
WHEN MONTH(sage) > MONTH(NOW()) OR (MONTH(sage) = MONTH(NOW()) AND DAY(sage) > DAY(NOW()))
THEN TIMESTAMPDIFF(YEAR, sage, NOW())
ELSE TIMESTAMPDIFF(YEAR, sage, NOW()) - 1
END AS 周岁
FROM Student;
47.查询本周过生日的学生
SELECT sid, sname,DAYOFYEAR(sage) as 一年第几周 FROM Student
WHERE WEEKOFYEAR(NOW())=WEEKOFYEAR(sage);
48.查询下周过生日的学生
SELECT sid, sname,DAYOFYEAR(sage) as 一年第几周 FROM Student
WHERE WEEKOFYEAR(NOW())+1=WEEKOFYEAR(sage);
49.查询本月过生日的学生
SELECT sid, sname,MONTH(sage) as 生日月份 FROM Student
WHERE MONTH(NOW())=MONTH(sage);
50.查询下月过生日的学生
SELECT sid, sname,MONTH(sage) as 生日月份 FROM Student
WHERE MONTH(NOW())+1=MONTH(sage);
五、问题总结
1.20题和21题需要调整数据,总分没有并列,看不出效果
2.30题总分平均没有并列,看不出效果
3.42题选课数量相同,看不出效果
4.45题学生表sage数据是出生日期(1990-01-01 00:00:00),应该改成字段birthday