1.查询001课程比002课程成绩高的所有学生的学号:
select a.sid,a.score as score1,b.score as score2 from (select sid,score from scores where cid = 1)as a left join (select sid,score from scores where cid = 2) as b on b.sid=a.sid where a.score>b.score
2、查询平均成绩大于60分的同学的学号和平均成绩:
select sid,avg(score)as avg_score from scores group by sid having avg_score>60
3、查询所有同学的学号,姓名,选课数,总成绩:
select sid,count(*),sum(score) from scores group by sid
4、查询姓李的老师个数
select count(*) from teacher where tname like '李%'
5、查询没有学过张三老师课的同学学号,姓名:
select a.sid,a.sname,b.score from (select sid,sname from student)as a left join (SELECT sid,score FROM scores where cid = (select cid from (select tid from teacher where tname='张三')as c left join (select cid,tid from course)as d on c.tid=d.tid) group by sid)as b on b.sid=a.sid where IFNULL(b.score,'null')=0
6、查询学过001和002课程的同学的姓名学号:
select a.sid,b.score as score1,c.score as score2,a.sname from ( (select sid,sname from student)as a left join (select sid,score from scores where cid=1)as b on b.sid=a.sid left join (select sid,score from scores where cid =2)as c on c.sid = a.sid) where b.score>0 and c.score>0
7、查询学过叶萍老师课的同学的学号,姓名:
select d.sid,d.sname from( (select distinct(sid) from scores where cid = (select b.cid from (select tid from teacher where tname = '张三')as a left join (select tid,cid from course)as b on b.tid=a.tid) )as c left join (select sid,sname from student)as d on d.sid=c.sid)
8、查询002成绩比001低的同学的学号,姓名:
select d.sid,c.score1,c.score2,d.sname from (select a.sid,a.score as score1,b.score as score2 from (select sid,score from scores where cid=1) as a left join (select sid,score from scores where cid =2)as b on b.sid = a.sid where a.score>b.score )as c left join (select sid,sname from student)as d on d.sid = c.sid
9、查询所有课程成绩小于60分的同学的学号
select c.sid,c.sname from (select sid,count(*)as tmp1 from scores group by sid)as a left join (select sid,count(*)as tmp2 from scores where score<60 group by sid) as b on b.sid=a.sid left join (select sid,sname from student) as c on c.sid = a.sid where a.tmp1=b.tmp2
10、查询没有学全 所有课的同学的学号和姓名
select b.sid,b.sname from (select sid,count(*) as course from scores group by sid having course<(select count(*) from course) )as a left join (select sid,sname from student) as b on b.sid = a.sid
11、查询至少有一门课程与学号1001的同学所学相同的同学的学号和姓名
select a.sid,b.sname from (select distinct(sid) from scores where cid in ((select group_concat(cid) from scores where sid = 1)) and sid !=1)as a left join (select sid,sname from student)as b on b.sid = a.sid;