Sql100
数据表介绍¶
学生表¶
Student(SId,Sname,Sage,Ssex)
-- SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
课程表¶
Course(CId,Cname,TId)
-- CId 课程编号,Cname 课程名称,TId 教师编号
教师表¶
Teacher(TId,Tname)
-- TId 教师编号,Tname 教师姓名
成绩表¶
SC(SId,CId,score)
-- SId 学生编号,CId 课程编号,score 分数
-- 学生表 Student
DROP TABLE IF EXISTS Student;
CREATE TABLE Student(SId VARCHAR(10),Sname VARCHAR(10),Sage DATETIME,Ssex VARCHAR(10));
INSERT INTO Student VALUES
('01', '赵雷', '1990-01-01', '男'),
('02', '钱电', '1990-12-21', '男'),
('03', '孙风', '1990-12-20', '男'),
('04', '李云', '1990-12-06', '男'),
('05', '周梅', '1991-12-01', '女'),
('06', '吴兰', '1992-01-01', '女'),
('07', '郑竹', '1989-01-01', '女'),
('08', '王菊', '1990-01-20', '女'),
('09', '张三', '2017-12-20', '女'),
('10', '李四', '2017-12-25', '女'),
('11', '李四', '2012-06-06', '女'),
('12', '赵六', '2013-06-13', '女'),
('13', '孙七', '2014-06-01', '女');
-- 科目表 Course
DROP TABLE IF EXISTS Course;
CREATE TABLE Course(CId VARCHAR(10),Cname nVARCHAR(10),TId VARCHAR(10));
INSERT INTO Course VALUES
('01', '语文', '02'),
('02', '数学', '01'),
('03', '英语', '03');
-- 教师表 Teacher
DROP TABLE IF EXISTS Teacher;
CREATE TABLE Teacher(TId VARCHAR(10),Tname VARCHAR(10));
INSERT INTO Teacher VALUES
('01', '张三'),
('02', '李四'),
('03', '王五');
-- 成绩表 SC
DROP TABLE IF EXISTS SC;
CREATE TABLE SC(SId VARCHAR(10),CId VARCHAR(10),score decimal(18,1));
INSERT INTO SC VALUES
('01', '01', 80),
('01', '02', 90),
('01', '03', 99),
('02', '01', 70),
('02', '02', 60),
('02', '03', 80),
('03', '01', 80),
('03', '02', 80),
('03', '03', 80),
('04', '01', 50),
('04', '02', 30),
('04', '03', 20),
('05', '01', 76),
('05', '02', 87),
('06', '01', 31),
('06', '03', 34),
('07', '02', 89),
('07', '03', 98);
练习题目¶
查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数¶
思路:把 Class1 的成绩连成一个表,Class2 的成绩连成一个表,取二者交集。
select * from Student RIGHT JOIN (
select t1.SId, class1, class2 from
(select SId, score as class1 from SC sc where sc.CId = '01')as t1,
(select SId, score as class2 from SC sc where sc.CId = '02')as t2
where t1.SId = t2.SId AND t1.class1 > t2.class2
)r
on Student.SId = r.SId;
查询同时存在" 01 "课程和" 02 "课程的情况¶
SELECT * FROM Student
WHERE SId IN (SELECT Student.SId FROM Student JOIN SC S on Student.SId = S.SId AND S.CId = '01')
AND SId IN (SELECT Student.SId FROM Student JOIN SC S2 on Student.SId = S2.SId AND S2.CId = '02');
查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )¶
SELECT * FROM Student INNER JOIN (SELECT SId, CId FROM SC WHERE CId = '01') AS class1 ON Student.SId = class1.SId
LEFT JOIN (SELECT SId, CId FROM SC WHERE CId='02') AS class2 ON Student.SId = class2.SId;
查询不存在" 01 "课程但存在" 02 "课程的情况¶
SELECT * FROM Student
WHERE SId NOT IN (SELECT Student.SId FROM Student JOIN SC S on Student.SId = S.SId AND S.CId = '01')
AND SId IN (SELECT Student.SId FROM Student JOIN SC S2 on Student.SId = S2.SId AND S2.CId = '02');
查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩¶
SELECT Student.SId, Student.Sname, x.avg_score
FROM Student
JOIN (
SELECT SId, AVG(score) AS avg_score
FROM SC
GROUP BY SId
HAVING AVG(score) >= 60
) AS x ON Student.SId = x.SId;
查询在 SC 表存在成绩的学生信息¶
SELECT * FROM Student
WHERE SId IN (
SELECT SId FROM SC
WHERE score IS NOT NULL
);
为了测试效果可以以下方法修改 SC 表
UPDATE SC SET score=NULL WHERE SId='07';
查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )¶
SELECT Student.SId, Sname, num_class, total_score
FROM Student LEFT JOIN (
SELECT SId, COUNT(CId) AS num_class, SUM(score) AS total_score
FROM SC
GROUP BY SId
) r on Student.SId = r.SId;
查有成绩的学生信息¶
本题考察IN
和EXISTS
的用法
SELECT * FROM Student
WHERE EXISTS( SELECT SC.SId FROM SC WHERE Student.SId = SC.SId );
SELECT * FROM Student
WHERE SId IN (SELECT DISTINCT SId FROM SC);
查询「李」姓老师的数量¶
SELECT COUNT(*) FROM Teacher
WHERE Tname LIKE '李%'
附:查询所有李姓老师
SELECT * FROM Teacher
WHERE Tname LIKE '李%'
查询学过「张三」老师授课的同学的信息¶
SELECT * FROM Student
WHERE SId IN (
SELECT SId
FROM SC JOIN Course C on SC.CId = C.CId
JOIN Teacher T on C.TId = (
SELECT Teacher.TId FROM Teacher
WHERE Tname = "张三"
)
)
select Student.* from Student,Teacher,Course,SC
where
Student.Sid = SC.Sid
and Course.cid = SC.Cid
and Course.tid = Teacher.Tid
and tname = '张三';
查询没有学全所有课程的同学的信息¶
SELECT * -- 对于某学生甲
FROM Student
WHERE EXISTS( -- 存在这样一个课程
SELECT *
FROM Course
WHERE NOT EXISTS( -- 所述学生甲没有选修
SELECT *
FROM SC
WHERE SC.SId = Student.SId AND SC.CId = Course.CId
)
);
查询至少有一门课与学号为"01"的同学所学相同的同学的信息¶
SELECT * FROM Student
WHERE SId in (
SELECT SId FROM SC
WHERE CId IN (
SELECT CId FROM SC
WHERE SId = '01'
)
);
把 01 自己也算进去了
查询和"01"号的同学学习的课程完全相同的其他同学的信息¶
select *
from Student
where SId in (select SId
from SC
where CId in (select distinct CId from SC where SId = '01')
and SId <> '01'
group by SId
having COUNT(CId) >= (
SELECT COUNT(*) FROM SC WHERE SId = '01'
))
-- 9. 查询和"01"号的同学学习的课程完全相同的其他同学的信息
select DISTINCT Student.*
from (
select Student.SId, t.CId
from Student,
(select SC.CId from SC where SC.SId = '01') as t) as t1
LEFT JOIN SC on t1.SId = SC.SId and t1.CId = SC.CId,
Student
where SC.SId is null and t1.SId = Student.SId;
这解法越来越玄学3。
查询没学过"张三"老师讲授的任一门课程的学生姓名¶
SELECT Sname FROM Student
WHERE SId NOT IN (
SELECT SId FROM SC
WHERE CId IN (
SELECT CId FROM Course
WHERE TId IN (
SELECT TId FROM Teacher
WHERE Tname = "张三"
)
)
);
查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩¶
SELECT Student.SId, Student.Sname, x.avg_score
FROM Student JOIN (
SELECT SId, AVG(score) AS avg_score
FROM SC
WHERE score < 60
GROUP BY SId
HAVING COUNT(*) >= 2
) AS x ON Student.Sid = x.SId;
检索"01"课程分数小于 60,按分数降序排列的学生信息¶
SELECT Student.SId SId, Sname, Sage, Ssex
FROM Student JOIN SC S on Student.SId = S.SId
WHERE S.CId = '01' AND S.score < 60
ORDER BY score DESC;
按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩¶
select SC.SId,
SC.CId,
SC.score,
t1.avgscore
from SC
left join (select SC.SId, avg(SC.score) as avgscore
from SC
GROUP BY SC.SId) as t1 on SC.SId = t1.SId
ORDER BY t1.avgscore DESC;
select SId,
max(case when CId='01' then score end) as '01',
max(case when CId='02' then score end) as '02',
max(case when CId='03' then score end) as '03',
avg(score)
from SC
group by SId
order by avg(score) desc
课程非常多或动态变化时此方法不可行。4
查询各科成绩最高分、最低分和平均分:¶
以如下形式显示:课程 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 and SC.SCore < 80 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;
按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺¶
按各科成绩进行排序,并显示排名, Score 重复时合并名次¶
查询学生的总成绩,并进行排名,总分重复时保留名次空缺¶
查询学生的总成绩,并进行排名,总分重复时不保留名次空缺¶
统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比¶
查询各科成绩前三名的记录¶
查询每门课程被选修的学生数¶
查询出只选修两门课程的学生学号和姓名¶
查询男生、女生人数¶
查询名字中含有「风」字的学生信息¶
查询同名同性学生名单,并统计同名人数¶
查询 1990 年出生的学生名单¶
查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列¶
查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩¶
查询课程名称为「数学」,且分数低于 60 的学生姓名和分数¶
查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)¶
查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数¶
查询不及格的课程¶
查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名¶
求每门课程的学生人数¶
成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩¶
成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩¶
查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩¶
查询每门功成绩最好的前两名¶
统计每门课程的学生选修人数(超过 5 人的课程才统计)。¶
检索至少选修两门课程的学生学号¶
查询选修了全部课程的学生信息¶
查询各学生的年龄,只按年份来算¶
按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一¶
查询本周过生日的学生¶
查询下周过生日的学生¶
查询本月过生日的学生¶
查询下月过生日的学生¶
补充¶
选修了所有课程的学生¶
SELECT * -- 对于某学生甲
FROM Student
WHERE NOT EXISTS( -- 不存在这样一个课程
SELECT *
FROM Course
WHERE NOT EXISTS( -- 所述学生甲没有选修
SELECT *
FROM SC
WHERE SC.SId = Student.SId AND SC.CId = Course.CId
)
);
去掉一个NOT
就可以做到查没有选修所有课程的学生
SELECT * -- 对于某学生甲
FROM Student
WHERE EXISTS( -- 存在这样一个课程
SELECT *
FROM Course
WHERE NOT EXISTS( -- 所述学生甲没有选修
SELECT *
FROM SC
WHERE SC.SId = Student.SId AND SC.CId = Course.CId
)
);