MySQL经典题目
MYSQL经典题目
学生表 Student
createtableStudent(SIdvarchar(10),Snamevarchar(10),Sagedatetime,Ssexvarchar(10));
insertintoStudentvalues('01' , '赵雷' , '1990-01-01' , '男');
insertintoStudentvalues('02' , '钱电' , '1990-12-21' , '男');
insertintoStudentvalues('03' , '孙风' , '1990-12-20' , '男');
insertintoStudentvalues('04' , '李云' , '1990-12-06' , '男');
insertintoStudentvalues('05' , '周梅' , '1991-12-01' , '女');
insertintoStudentvalues('06' , '吴兰' , '1992-01-01' , '女');
insertintoStudentvalues('07' , '郑竹' , '1989-01-01' , '女');
insertintoStudentvalues('09' , '张三' , '2017-12-20' , '女');
insertintoStudentvalues('10' , '李四' , '2017-12-25' , '女');
insertintoStudentvalues('11' , '李四' , '2012-06-06' , '女');
insertintoStudentvalues('12' , '赵六' , '2013-06-13' , '女');
insertintoStudentvalues('13' , '孙七' , '2014-06-01' , '女');
科目表 Course
createtableCourse(CIdvarchar(10),Cnamenvarchar(10),TIdvarchar(10));
insertintoCoursevalues('01' , '语文' , '02');
insertintoCoursevalues('02' , '数学' , '01');
insertintoCoursevalues('03' , '英语' , '03');
教师表 Teacher
createtableTeacher(TIdvarchar(10),Tnamevarchar(10));
insertintoTeachervalues('01' , '张三');
insertintoTeachervalues('02' , '李四');
insertintoTeachervalues('03' , '王五');
成绩表 SC
createtableSC(SIdvarchar(10),CIdvarchar(10),scoredecimal(18,1));
insertintoSCvalues('01' , '01' , 80);
insertintoSCvalues('01' , '02' , 90);
insertintoSCvalues('01' , '03' , 99);
insertintoSCvalues('02' , '01' , 70);
insertintoSCvalues('02' , '02' , 60);
insertintoSCvalues('02' , '03' , 80);
insertintoSCvalues('03' , '01' , 80);
insertintoSCvalues('03' , '02' , 80);
insertintoSCvalues('03' , '03' , 80);
insertintoSCvalues('04' , '01' , 50);
insertintoSCvalues('04' , '02' , 30);
insertintoSCvalues('04' , '03' , 20);
insertintoSCvalues('05' , '01' , 76);
insertintoSCvalues('05' , '02' , 87);
insertintoSCvalues('06' , '01' , 31);
insertintoSCvalues('06' , '03' , 34);
insertintoSCvalues('07' , '02' , 89);
insertintoSCvalues('07' , '03' , 98);
具体SQL
# 查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
#首先需要查询出01课程和02课程各自对应的学生id
#然后筛选相同id,判断一下成绩,之后关联student表进行查询
SELECT Student.SId,
Student.Sname,
sc1.score,
sc2.score
FROM Student,
(SELECT * FROM SC where SC.CId = '01') AS sc1,
(SELECT * FROM SC where SC.CId = '02') AS sc2
WHERE sc1.SId = sc2.SId
and Student.SId = sc1.SId
and sc1.score > sc2.score
GROUPBY Student.SId;
select *
from (
select st1.SId,
st1.score1,
st2.score2
from (select SC.SId, SC.scoreas score1 from SC SC where SC.CId = '01') as st1,
(select SC.SId, SC.scoreas score2 from SC SC where SC.CId = '02') as st2
where st1.SId = st2.SId
and score1 > score2
) r
LEFT JOIN Student ON r.SId = Student.SId
# 1.1 查询同时存在" 01 "课程和" 02 "课程的情况
SELECT a.sid1
FROM (SELECT SId AS sid1 FROM SC where CId = '01') AS a,
(SELECT SId AS sid2 FROM SC where CId = '02') AS b
where a.sid1 = b.sid2;
# 1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )
SELECT *
FROM (SELECT * FROM SC WHERE SC.CId = '01') AS sc1
LEFT JOIN
(SELECT * FROM SC WHERE SC.CId = '02') AS sc2
ON sc1.SId = sc2.SId;
# sql合并成一行来进行比对
select *
from (select * from SC where SC.CId = '01') as t1
left join
(select * from SC where SC.CId = '02') as t2
on t1.SId = t2.SId;
# 1.3 查询不存在" 01 "课程但存在" 02 "课程的情况
select *
from SC
WHERE SC.SIdNOTIN (select SC.SIdfrom SC where SC.CId = '01')
AND SC.CId = '02';
# 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
SELECT Student.SId,
Student.Sname,
AVG(SC.score)
FROM Student,
SC
WHERE Student.SId = SC.SId
GROUPBY Student.SId
HAVING AVG(SC.score > 60);
# 查询在 SC 表存在成绩的学生信息
SELECT Student.SId,
Sname,
Sage,
Ssex
FROM Student,
SC
where Student.SId = SC.SId
and SC.scoreisnotnull
GROUPBY Student.SId
# 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
SELECT Student.SId,
Student.Sname,
COUNT(SC.CId),
SUM(SC.score)
FROM Student
LEFT JOIN
SC
ON Student.SId = SC.SId
GROUPBY Student.SId;
# 4.1 查有成绩的学生信息
SELECTDISTINCT Student.SId,
Sname,
Sage,
Ssex
FROM Student,
SC
where Student.SId = SC.SId
GROUPBY Student.SId;
#exists用在B表数据量大的情况下,因为他底层只会返回一个true或者false
SELECT *
FROM Student
where exists(SELECT * from SC where Student.SId = SC.SIdand SC.scoreisnotnull)
#in用在B表数据量小的情况下 因为他底层是需要for循环的
SELECT *
FROM Student
where Student.SIDin (SELECT SID from SC where Student.SId = SC.SIdand SC.scoreisnotnull)
# 查询「李」姓老师的数量
SELECTCOUNT(*)
FROM Teacher
WHERE Tname like'李%'
# 查询学过「张三」老师授课的同学的信息
SELECT Student.*
FROM Student,
SC,
Teacher,
Course
WHERE Student.SId = SC.SId
AND Course.TId = Teacher.TId
AND Course.CId = SC.CId
AND Teacher.Tname = '张三';
SELECT *
FROM Student
where SId IN (SELECT SId
FROM SC
WHERE SC.CId = (
SELECT Course.CId
FROM Course,
(SELECT TId
FROM Teacher
where Tname = '张三') AS t
where Course.TId = t.TId))
# 查询没有学全所有课程的同学的信息
SELECT Student.*
FROM Student,
SC
where Student.SId = SC.SId
GROUPBY Student.SId
HAVINGCOUNT(SC.CId) < (SELECTcount(*) AS c1 FROM Course);
# 查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
SELECT Student.*
FROM Student,
SC
WHERE Student.SId = SC.SId
and SC.CIdIN (SELECT Course.CIdAS cid
FROM SC,
Course
WHERE Course.CId = SC.CId
AND SC.SId = '01')
GROUPBY Student.SId;
# 查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息
SELECT s.*
FROM Student s
WHERE sid IN
( SELECT sid FROM SC WHERE sid != 01
AND cid IN (SELECT cid FROM SC WHERE sid = 01)
GROUPBY sid
HAVINGCOUNT(cid) = (SELECTCOUNT(cid) FROM SC WHERE sid = 01));
-- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
-- 首先查询和"01"号同学学习的课程次数相同的同学,然后再查询出学了除去01号同学学的课程以后的东西的同学,对第一次的人进行一个取反
SELECT * FROM Student WHERE SId IN (
SELECT SC.SIdFROM SC GROUPBY SC.SIdHAVINGCOUNT(CId) =
(SELECT
COUNT(CId)
FROM
SC
where SId = '01'))
AND
SId NOTIN (
SELECT SC.SIdFROM SC WHERE CId NOTIN (SELECT CId FROM SC WHERE SId = '01') GROUPBY SC.SId)
AND Student.SId != '01';
# 查询没学过"张三"老师讲授的任一门课程的学生姓名
SELECT Student.Sname
FROM Student
where SId NOTin (
SELECT SC.SId
FROM SC,
Teacher,
Course
WHERE Course.TId = Teacher.TId
AND Course.CId = SC.CId
AND Teacher.Tname = '张三'
);
# 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECT
Student.SId,
Student.Sname,
AVG(SC.score)
FROM
Student,
SC
where
Student.SId = SC.SId
AND SC.score < 60
groupby Student.SId
HAVINGCOUNT(SC.score) >= 2
select Student.SId, Student.Sname,b.avg
from Student RIGHT JOIN
(select sid, AVG(score) as avg from SC
where sid in (
select sid from SC
where score<60
GROUPBY sid
HAVINGcount(score)>1)
GROUPBY sid) b on Student.sid=b.sid;
# 检索" 01 "课程分数小于 60,按分数降序排列的学生信息 降序 = 从大到小
SELECT
Student.*
FROM
Student,
SC
WHERE
Student.SId = SC.SId
AND
SC.CId = '01'AND SC.score < 60
GROUPBY Student.SId
ORDERBY SC.scoreDESC
# 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
SELECT
Student.SId,
Student.Sname,
Student.Sage,
Student.Ssex,
SUM(S.score),
AVG(S.score)
FROM
Student
LEFT JOIN SC S on Student.SId = S.SId
GROUPBY Student.SId
ORDERBY AVG(S.score) DESC