--1、查询01课程比02课程成绩高的所有学生的学号select sc1.sid,sc1.score score1,sc2.score score2
from sc sc1
join sc sc2 on sc1.sid=sc2.sid
where sc1.cid =1and sc2.cid =2and sc1.score>sc2.score
--2、查询平均成绩大于60分的同学的学号和平均成绩select sid,avg(score) avgscore
from sc
groupby sid
having avgscore>60;--3、查询所有同学的学号、姓名、选课数、总成绩select stu.sid,stu.sname,count(sc.cid) countcourse,casewhensum(sc.score)isnullthen0elsesum(sc.score)end sumscore
from student stu
leftjoin sc
on sc.sid=stu.sid
groupby stu.sid,stu.sname;--4、查询姓‘李’的老师的个数:selectcount(*)from teacher where tname like'李%';--5、查询没有学过“张三”老师课程的同学的学号、姓名:select stu.sid,sname
from student stu
join course cs
join teacher t
leftjoin sc on stu.sid=sc.sid and cs.cid=sc.cid and t.tid=cs.tid
where tname='张三'groupby stu.sid,sname
havingsum(casewhen sc.sid isnullthen0else1end)=0;--6、查询学过“张三”老师所教的所有课的同学的学号、姓名:select stu.sid,sname
from student stu
join course cs
join teacher t
leftjoin sc on stu.sid=sc.sid and cs.cid=sc.cid and t.tid=cs.tid
where tname='张三'groupby stu.sid,sname
havingsum(casewhen sc.sid isnullthen1else0end)=0;--7、查询学过01并且也学过编号02课程的同学的学号、姓名:select stu.sid,stu.sname
from student stu
join sc sc1 on sc1.sid=stu.sid
join sc sc2 on sc2.sid=sc1.sid
where sc1.cid=01and sc2.cid=02;--8、查询课程编号02的成绩比课程编号01课程成绩低的所有同学的学号、姓名:select stu.sid,stu.sname
from student stu
join sc sc1 on sc1.sid=stu.sid and sc1.cid=01leftjoin sc sc2 on sc2.sid=sc1.sid and sc2.cid=02