表描述:
–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 分数
–创建测试数据
create table Student(SID varchar(10),Sname nvarchar(10),Sage datetime,Ssex nvarchar(10));
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’ , ‘女’);
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’);
create table Teacher(TID varchar(10),Tname nvarchar(10));
insert into Teacher values(‘01’ , ‘张三’);
insert into Teacher values(‘02’ , ‘李四’);
insert into Teacher values(‘03’ , ‘王五’);
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);
=====================================================================================
数据表可视化
表:student
SId | Sname | Sage | Ssex |
---|---|---|---|
01 | 赵雷 | 1990/1/1 00:00:00 | 男 |
02 | 钱电 | 1990/12/21 00:00:00 | 男 |
03 | 孙风 | 1990/12/20 00:00:00 | 男 |
04 | 李云 | 1990/12/6 00:00:00 | 男 |
05 | 周梅 | 1991/12/1 00:00:00 | 女 |
06 | 吴兰 | 1992/1/1 00:00:00 | 女 |
07 | 郑竹 | 1989/1/1 00:00:00 | 女 |
09 | 张三 | 2017/12/20 00:00:00 | 女 |
10 | 李四 | 2017/12/25 00:00:00 | 女 |
11 | 李四 | 2012/6/6 00:00:00 | 女 |
12 | 赵六 | 2013/6/13 00:00:00 | 女 |
13 | 孙七 | 2014/6/1 00:00:00 | 女 |
表:course
CId | Cname | TId |
---|---|---|
01 | 语文 | 02 |
02 | 数学 | 01 |
03 | 英语 | 03 |
表:sc
SId | CId | score |
---|---|---|
01 | 01 | 80.0 |
01 | 02 | 90.0 |
01 | 03 | 99.0 |
02 | 01 | 70.0 |
02 | 02 | 60.0 |
02 | 03 | 80.0 |
03 | 01 | 80.0 |
03 | 02 | 80.0 |
03 | 03 | 80.0 |
04 | 01 | 50.0 |
04 | 02 | 30.0 |
04 | 03 | 20.0 |
05 | 01 | 76.0 |
05 | 02 | 87.0 |
06 | 01 | 31.0 |
06 | 03 | 34.0 |
07 | 02 | 89.0 |
07 | 03 | 98.0 |
表:teacher
TId | Tname |
---|---|
01 | 张三 |
02 | 李四 |
03 | 王五 |
=====================================================================================
查询查询课程01分数比课程02分数高的学生信息
SELECT
s.SId,
s.Sname,
s.Ssex,
sc.CId,
c.Cname,
sc.score
FROM student s
JOIN sc ON s.SId = sc.SId
JOIN course c ON sc.CId = c.CId
JOIN teacher t ON c.TId = t.TId
WHERE s.SId IN
(
SELECT t1.SId
FROM
(SELECT SId, score FROM sc WHERE CId = '01') t1
JOIN
(SELECT SId, score FROM sc WHERE CId = '02') t2
ON t1.SId = t2.SId
WHERE t1.score > t2.score
) #此处筛选符合条件的学号
;
查询查询课程01分数比课程02分数高的学生信息(此处认为只存在课程01,课程02不存在情况也符合条件)
SELECT
s.SId,
s.Sname,
s.Ssex,
sc.CId,
c.Cname,
sc.score
FROM student s
JOIN sc ON s.SId = sc.SId
JOIN course c ON sc.CId = c.CId
JOIN teacher t ON c.TId = t.TId
WHERE s.SId IN
(
SELECT t1.SId
FROM
(SELECT SId, score FROM sc WHERE CId = '01') t1
LEFT JOIN
(SELECT SId, score FROM sc WHERE CId = '02') t2
ON t1.SId = t2.SId
WHERE t1.score > IF(t2.score IS NULL, -1, t2.score) #课程02不存在时score赋为-1
) #此处筛选符合条件的学号
;
查询个人总成绩并进行排名并按名次升序排名,同分同名次
SELECT
s.SId,
s.Sname,
t_sum.`总成绩`,
(
SELECT COUNT(DISTINCT t1.`总成绩`)
FROM
(
SELECT sc.SId, SUM(sc.score) AS `总成绩`
FROM sc
GROUP BY sc.SId
) t1
WHERE t_sum.`总成绩` <= t1.`总成绩`
) AS rank
FROM student s
JOIN
(
SELECT sc.SId, SUM(sc.score) AS `总成绩`
FROM sc
GROUP BY sc.SId
) t_sum
ON s.SId = t_sum.SId
ORDER BY rank
;
查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
SELECT
sc.CId AS '课程ID',
c.Cname AS '课程名',
MAX(sc.score) AS '最高分',
MIN(sc.score) AS '最低分',
AVG(sc.score) AS '平均分',
SUM((CASE WHEN sc.score >= 60 THEN 1 ELSE 0 END)) / COUNT(sc.SId) AS '及格率',
SUM((CASE WHEN sc.score BETWEEN 70 AND 79 THEN 1 ELSE 0 END)) / COUNT(sc.SId) AS '中等率',
SUM((CASE WHEN sc.score BETWEEN 80 AND 89 THEN 1 ELSE 0 END)) / COUNT(sc.SId) AS '优良率',
SUM((CASE WHEN sc.score >= 90 THEN 1 ELSE 0 END)) / COUNT(sc.SId) AS '优秀率'
FROM sc
JOIN course c
ON sc.CId = c.CId
GROUP BY sc.CId
;
统计各科成绩各分数段人数:课程编号,课程名称, 100-85 , 85-70 , 70-60 , 0-60 及所占百分比
SELECT
sc.CId,
c.Cname,
SUM(CASE WHEN sc.score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS "[85, 100]人数",
SUM(CASE WHEN sc.score BETWEEN 70 AND 84 THEN 1 ELSE 0 END) AS "[70, 85 )人数",
SUM(CASE WHEN sc.score BETWEEN 60 AND 69 THEN 1 ELSE 0 END) AS "[60, 70 )人数",
SUM(CASE WHEN sc.score BETWEEN 0 AND 60 THEN 1 ELSE 0 END) AS "[0 , 60 )人数",
SUM(CASE WHEN sc.score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) / COUNT(sc.SId) AS "[85, 100]人数占比",
SUM(CASE WHEN sc.score BETWEEN 70 AND 84 THEN 1 ELSE 0 END) / COUNT(sc.SId) AS "[70, 85)人数占比",
SUM(CASE WHEN sc.score BETWEEN 60 AND 69 THEN 1 ELSE 0 END) / COUNT(sc.SId) AS "[60, 70)人数占比",
SUM(CASE WHEN sc.score BETWEEN 0 AND 60 THEN 1 ELSE 0 END) / COUNT(sc.SId) AS "[0 , 60)人数占比"
FROM sc
JOIN course c
ON sc.CId = c.CId
GROUP BY CId;
查询所有课程的成绩第2名到第3名的学生信息及该课程成绩,按课程名、rank升序排序(同分同名次)
SELECT
s.SId,
s.Sname,
s.Ssex,
c.Cname,
t2.score,
t3.rank
FROM student s
JOIN sc t2
on s.SId = t2.SId
JOIN
(
SELECT
SId,
CId,
(SELECT COUNT(DISTINCT score) FROM sc WHERE CId = t1.CId AND score >= t1.score) as rank #计算科目内排名
FROM
sc t1
WHERE
(SELECT COUNT(DISTINCT score) FROM sc WHERE CId = t1.CId AND score >= t1.score) BETWEEN 2 AND 3
) t3 #筛选科目排名为2或3的记录
ON t2.SId = t3.SId
AND t2.CId = t3.CId
JOIN course c
ON t2.CId = c.CId
ORDER BY c.Cname, t3.rank;
各科排名前三学生信息,按科目、科目排名降序排序
SELECT
s.SId,
s.Sname,
s.Ssex,
c.Cname,
t1.score,
t1.rank AS '科目排名'
FROM student s
JOIN
(
SELECT
CId,
SId,
score,
(SELECT COUNT(DISTINCT score) FROM sc WHERE t1.CId = CId AND score >= t1.score) AS rank
FROM sc t1
WHERE (SELECT COUNT(DISTINCT score) FROM sc WHERE t1.CId = CId AND score >= t1.score) <= 3
) t1
ON s.SId = t1.SId
JOIN course c
ON t1.CId = c.CId
ORDER BY c.Cname, t1.rank
;
各科排名前三学生信息,按科目、科目排名降序排序(横版显示)
SELECT
s.SId,
s.Sname,
s.Ssex,
MAX(CASE WHEN c.Cname = '语文' THEN t1.score END) AS '语文分数',
MAX(CASE WHEN c.Cname = '语文' THEN t1.rank END) AS '语文排名',
MAX(CASE WHEN c.Cname = '数学' THEN t1.score END) AS '数学分数',
MAX(CASE WHEN c.Cname = '数学' THEN t1.rank END) AS '数学排名',
MAX(CASE WHEN c.Cname = '英语' THEN t1.score END) AS '英语分数',
MAX(CASE WHEN c.Cname = '英语' THEN t1.rank END) AS '英语排名'
FROM student s
JOIN
(
SELECT
CId,
SId,
score,
(SELECT COUNT(DISTINCT score) FROM sc WHERE t1.CId = CId AND score >= t1.score) AS rank
FROM sc t1
WHERE (SELECT COUNT(DISTINCT score) FROM sc WHERE t1.CId = CId AND score >= t1.score) <= 3
) t1
ON s.SId = t1.SId
JOIN course c
ON t1.CId = c.CId
GROUP BY s.SId
ORDER BY c.Cname, t1.rank
;