SELECT*FROM(SELECT c.CId, c.Cname, sc.score, s2.Sname name1
FROM course c,
sc sc
RIGHTJOIN student s2 ON sc.SId = s2.SId
WHERE c.CId = sc.CId
GROUPBY c.CId, c.Cname, s2.Sname
HAVING c.CId ='01') t1,(SELECT c.CId, c.Cname, sc.score, s.Sname name2
FROM course c,
student s,
sc sc
WHERE s.SId = sc.SId
AND c.CId = sc.CId
GROUPBY c.CId, c.Cname, s.Sname
HAVING c.CId ='02') t2
WHERE t1.name1 = t2.name2
HAVING t1.score > t2.score;SELECT st.*, class1, class2
FROM student st
JOIN(SELECT*FROM(SELECT SId s1, score class1 FROM sc WHERE CId ='01') c1,(SELECT SId s2, score class2 FROM sc WHERE CId ='02') c2
WHERE s1 = s2
AND class1 > class2) c
ON st.SId = c.s1;SELECT*FROM sc sc,
course c
WHERE c.CId = sc.CId
AND c.CId ='02';SELECT*FROM student;SELECT*FROM course;SELECT*FROM sc;
1.1 查询同时存在" 01 “课程和” 02 "课程的情况
SELECT*FROM(SELECT c.CId, c.Cname, sc.score, s2.Sname name1
FROM course c,
sc sc
RIGHTJOIN student s2 ON sc.SId = s2.SId
WHERE c.CId = sc.CId
GROUPBY c.CId, c.Cname, s2.Sname
HAVING c.CId ='01') t1,(SELECT c.CId, c.Cname, sc.score, s.Sname name2
FROM course c,
student s,
sc sc
WHERE s.SId = sc.SId
AND c.CId = sc.CId
GROUPBY c.CId, c.Cname, s.Sname
HAVING c.CId ='02') t2
WHERE t1.name1 = t2.name2;
1.2 查询存在" 01 “课程但可能不存在” 02 "课程的情况(不存在时显示为 null )
SELECT*FROM((SELECT*FROM sc WHERE CId ='01') t1
LEFTJOIN(SELECT*FROM sc WHERE CId ='02') t2
ON
t1.Sid = t2.Sid);
1.3 查询不存在" 01 “课程但存在” 02 "课程的情况
SELECT*FROM sc
WHERE sc.CId ='02'AND sc.SId NOTIN(SELECT SId FROM sc WHERE sc.CId ='01');
2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
SELECT*,avg(sc.score) avgs
FROM student s,
sc sc
WHERE s.SId = sc.SId
GROUPBY s.Sname
HAVING avgs >60;
3.查询在 SC 表存在成绩的学生信息
SELECT*FROM sc sc,
student s
WHERE sc.SId = s.SId
GROUPBY s.Sname;SELECT*FROM student
WHERE SId IN(SELECTDISTINCT sid FROM sc);
4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的成绩总和
SELECT s.SId, s.Sname,count(*)'选课总数',sum(sc.score)'成绩总和'FROM student s,
sc sc,
course c
WHERE s.SId = sc.SId
AND c.CId = sc.CId
GROUPBY s.Sname;
4.1显示没选课的学生(显示为NULL)
SELECT*FROM student s,
course c,
sc sc
WHERE s.SId = sc.SId
AND sc.CId = c.CId
GROUPBY c.Cname;SELECT*FROM(SELECT Sname, SId FROM student) t1
LEFTJOIN(SELECT CId, SId FROM sc) t3 ON t1.SId = t3.sid
GROUPBY t1.SId;SELECT st.sid,
st.Sname,CASEWHENCOUNT(sc.SId)>0THENCOUNT(sc.SId)ELSENULLEND'nums',SUM(sc.score)'sum'FROM student st
LEFTJOIN sc ON st.SId = sc.SId
GROUPBY st.SId;
4.2查有成绩的学生信息
SELECT*FROM(SELECT Sname, SId FROM student) t1
LEFTJOIN(SELECT CId, SId, score FROM sc) t3 ON t1.SId = t3.sid
GROUPBY t3.SId
HAVING t3.score ISNOTNULL;SELECT*FROM student
WHERE SId IN(SELECTDISTINCT SId FROM sc);SELECT*FROM student s
WHEREexists(SELECT*FROM sc WHERE sc.SId = s.SId);SELECT*FROM student
WHERE SId IN(SELECTDISTINCT SId FROM sc);-- 适用于右表小SELECT*FROM student st
WHEREEXISTS(SELECT*FROM sc WHERE sc.SId = st.SId);
5.查询「李」姓老师的数量
SELECTcount(*)FROM teacher
WHERE Tname LIKE'李%';
6.查询学过「张三」老师授课的同学的信息
SELECT s.SId, s.Sname, s.Sage, s.Ssex, t.Tname
FROM teacher t,
student s,
course c,
sc sc
WHERE s.SId = sc.sid
AND sc.CId = c.CId
AND c.TId = t.TId
AND t.Tname ='张三';
7.查询没有学全所有课程的同学的信息
SELECT t1.SId, t1.Sname, t1.Ssex, t1.Sage,count(*)FROM student t1
LEFTJOIN
sc t2 ON t1.SId = t2.SId
GROUPBY t1.SId
HAVINGcount(*)<3;SELECT*FROM student
WHERE sid NOTIN(SELECT sid FROM sc GROUPBY sid HAVINGcount(*)=(SELECTcount(*)FROM course));SELECT*FROM student
WHERE SId NOTIN(SELECT SId FROM sc GROUPBY SId HAVINGCOUNT(*)=(SELECTCOUNT(*)FROM course));
8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
SELECT*FROM student
WHERE sid IN(SELECT sid FROM sc WHERE cid IN(SELECT CId FROM sc WHERE sid ='01'));
9.查询和" 01 "号的同学学习的课程完全相同的其他同学的信息
解法一
SELECT*FROM student
WHERE SId IN(SELECT sid
FROM(SELECT sid, group_concat(cid ORDERBY cid) cc FROM sc GROUPBY sid) tt
WHERE tt.cc IN(SELECT group_concat(CId ORDERBY CId)FROM sc WHERE sid ='01'));
解法二
SELECT*FROM student
WHERE SId IN(SELECT SId
FROM sc
WHERE CId IN(SELECT CId FROM sc WHERE SId ='01')AND SId <>'01'GROUPBY SId
HAVINGCOUNT(*)=(SELECTCOUNT(*)FROM sc WHERE SId ='01'));
10.查询没学过"张三"老师讲授的任一门课程的学生姓名
SELECT*FROM student
WHERE sid NOTIN(SELECT sid
FROM sc
WHERE CId IN(SELECT c.CId
FROM course c,
teacher t
WHERE c.CId = t.TId
AND t.Tname ='张三'));
11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECT*FROM(SELECT s.SId, s.Sname, s.Sage, s.Ssex,count(*) x,avg(score)FROM sc,
student s
WHERE score <=60AND s.SId = sc.sid
GROUPBY SId) t
WHERE t.x >=2;SELECT st.SId, sname,avg(score)FROM student st
JOIN sc ON st.sid = sc.SId
GROUPBY st.SId
HAVINGCOUNT(score <=60ORNULL)>=2;
12.检索" 01 "课程分数小于 60,按分数降序排列的学生信息
SELECT s.SId, s.Sname, s.Sage, s.Ssex, t.score
FROM(SELECT*FROM sc WHERE score <60AND CId ='01'GROUPBY SId) t,
student s
WHERE t.sid = s.SId
ORDERBY t.score DESC;
13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
SELECT*FROM sc s
LEFTJOIN(SELECT*,avg(score) avg FROM sc GROUPBY sc.SId) t ON s.sid = t.sid
ORDERBY avg DESC;SELECT*FROM sc
JOIN(SELECT SId,avg(score) avg FROM sc GROUPBY SId) avgs
ON sc.SId = avgs.SId
ORDERBY avgs.avg DESC;
SELECT s.SId, s.Sname,avg(score) avg_score
FROM sc,
student s
WHERE sc.SId = s.SId
GROUPBY sid
HAVING avg_score >=85;
27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
SELECT s.sid, s.Sname, score
FROM course c,
sc,
student s
WHERE c.Cname ='数学'AND c.cid = sc.CId
AND score <60AND s.sid = sc.SId;
28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
SELECT s.SId, s.Sname, score, cid,(SELECT c.Cname FROM course c WHERE c.CId = sc.cid) name
FROM student s
LEFTJOIN sc ON s.SId = sc.SId;
29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
这个我没太理解,理解一是任意一门成绩均在70分以上
SELECT s.sid, s.sname, c.Cname, score
FROM sc,
course c,
student s
WHERE score >70AND c.CId = sc.CId
AND s.SId = sc.SId;SELECT st.Sname, c.Cname, s.score
FROM student st,
sc s,
course c
WHERE st.SId = s.SId
AND s.cid = c.cid
AND st.SId IN(SELECT sid FROM sc GROUPBY SId HAVINGMIN(score)>70);
理解二是存在一门成绩在70分以上即可满足条件
理解三就是找出所有大于70分的得分。
30.查询存在不及格的课程
SELECT cid FROM sc GROUPBY cid HAVINGmin(score)<60;
31.查询课程编号为 01 且课程成绩在 80 分及以上的学生的学号和姓名
SELECT s.sid, s.sname FROM sc,student s where sc.CId=01and sc.score>=80and s.SId=sc.sid;
32.求每门课程的学生人数
SELECT cid,count(*)FROM sc GROUPBY cid;
33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
SELECT s.sid,s.Sname,s.Ssex,s.Sage ,max(score) score FROM sc ,student s where CId=(SELECT cid FROM teacher t,course c where t.Tname='张三'and t.TId=CId)and s.sid=sc.SId;
34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
SELECT s.sid,s.Sname,s.Ssex,s.Sage ,max(score)OVER() score FROM sc ,student s where CId=(SELECT cid FROM teacher t,course c where t.Tname='张三'and t.TId=CId)and s.sid=sc.SId;
35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
这个问题其实一开始没太明白啥意思,后来理解为某个人的几科分数是一样的,需要把这个人找出来
select a.cid, a.sid, a.score from sc as a
innerjoin
sc as b
on a.sid = b.sid
and a.cid != b.cid
and a.score = b.score
groupby cid, sid;
36.查询每门功成绩最好的前两名
SELECT o.*FROM sc o where(SELECTCOUNT(*)FROM sc WHERE CId = o.CId AND score > o.score)+1<=2ORDERBY o.cid, sid;