-- 1、查询“c001”课程比“c002”课程成绩高的所有学生的学号;
SELECT a.sno '编号',a.score '语文成绩',b.score '数学成绩' from (SELECT sno,score FROM sc WHERE cno = 100) a LEFT JOIN
(SELECT sno,score FROM sc a WHERE cno = 200) b on a.sno = b.sno WHERE a.score > b.score
-- 2、查询平均成绩大于60 分的同学的学号和平均成绩;
SELECT sno '编号',avg(score) '成绩' FROM sc GROUP BY sno HAVING avg(score) > 82
-- 3、查询所有同学的学号、姓名、选课数、总成绩;
SELECT a.s_num,a.s_name,count(sno),sum(score) FROM student a,sc b WHERE a.s_num = b.sno
GROUP BY a.id,a.s_num
SELECT a.s_num,a.s_name,count(sno),sum(score) FROM student a,sc b WHERE a.s_num = b.sno
GROUP BY a.id
-- 4、查询姓“刘”的老师的个数;
SELECT count(1) FROM teacher where tname LIKE '何%'
-- 5、查询没学过“谌燕”老师课的同学的学号、姓名;
SELECT a.s_num,a.s_name,d.tname FROM student a LEFT JOIN sc b ON a.s_num = b.sno LEFT JOIN course c on b.cno = c.cno
LEFT JOIN teacher d on c.tno = d.tno GROUP BY a.s_num,d.tname HAVING d.tname = "黄老师"
-- 6、查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名;
SELECT DISTINCT(a.s_num),a.s_name,c.cname FROM student a LEFT JOIN sc b ON a.s_num = b.sno LEFT JOIN course c on b.cno = c.cno
LEFT JOIN teacher d on c.tno = d.tno where c.cname = ALL('语文' ,'化学')
select DISTINCT(a.s_num),a.s_name,b.cno from student a LEFT JOIN sc b on a.s_num = b.sno WHERE b.cno in (100,200)
select a.s_num,a.s_name,b.cno from student a LEFT JOIN sc b on a.s_num = b.sno WHERE b.cno = 200 and b.cno = 300
GROUP BY a.s_num WHERE
-- 7、查询学过“谌燕”老师所教的所有课的同学的学号、姓名;
SELECT DISTINCT(sc.sno) from sc
SELECT DISTINCT(a.s_num),a.s_name,c.cname,d.tname FROM student a LEFT JOIN sc b ON a.s_num = b.sno LEFT JOIN course c on b.cno = c.cno
LEFT JOIN teacher d on c.tno = d.tno GROUP BY a.s_num,d.tname HAVING d.tname = '何老师'
SELECT DISTINCT(a.s_num),a.s_name,c.cname,d.tname FROM student a LEFT JOIN sc b ON a.s_num = b.sno LEFT JOIN course c on b.cno = c.cno
LEFT JOIN teacher d on c.tno = d.tno GROUP BY a.s_num,d.tname HAVING d.tname = '许老师'
-- 8、查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名;
select g.sno,r.s_name,g.score FROM (SELECT a.sno,a.score FROM (select * FROM sc where cno = '200')
a LEFT JOIN (select * FROM sc where cno = '300')
b on a.sno = b.sno WHERE a.score > b.score) g LEFT JOIN student r on g.sno = r.s_num
-- 9、查询所有课程成绩小于60分的同学的学号、姓名
SELECT a.s_num,a.s_name,c.cname,b.score FROM student a LEFT JOIN sc b ON a.s_num = b.sno LEFT JOIN course c on b.cno = c.cno
LEFT JOIN teacher d on c.tno = d.tno GROUP BY a.s_num,c.cname HAVING b.score > 70
-- 10、查询没有学全所有课的同学的学号、姓名;
SELECT a.s_num,a.s_name,count(sno),sum(score) FROM student a,sc b WHERE a.s_num = b.sno
GROUP BY a.id HAVING count(sno) <> 6
select sno,avg(score) from sc
where score<80
group by sno
having count(sno)>2