本习题仅做练习使用!!!!
练习表如下(以下练习均以此表为准)





-- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
select s.*, s1.s_score, s2.s_score from score s1, score s2, student s where s1.c_id='01' and s2.c_id='02' and s1.s_id=s.s_id and s2.s_id=s.s_id and s1.s_score>s2.s_score
-- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
select s.*, s1.s_score, s2.s_score from score s1, score s2, student s where s1.c_id='01' and s2.c_id='02' and s1.s_id=s.s_id and s2.s_id=s.s_id and s1.s_score<s2.s_score
-- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select avg(s_score), s_id from score GROUP BY s_id having avg(s_score) >= 60
select s.s_id, s.s_name, a.avgscore from student s, (select avg(s_score) avgscore, s_id from score GROUP BY s_id having avg(s_score) >= 60) a where s.s_id=a.s_id
-- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
select s.s_id, s.s_name, a.avgscore from student s, (select avg(s_score) avgscore, s_id from score GROUP BY s_id having avg(s_score) < 60) a where s.s_id=a.s_id
-- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select count(*) cou, sum(s_score) sumscore, s_id from score GROUP BY s_id
select s.s_id, s.s_name, a.cou, a.sumscore from student s, (select count(*) cou, sum(s_score) sumscore, s_id from score GROUP BY s_id) a where s.s_id=a.s_id;
-- 6、查询"李"姓老师的数量
select count(*) from teacher where t_name like '李%'
-- 7、查询学过"张三"老师授课的同学的信息
select c.c_id from teacher t, course c where t.t_id=c.t_id and t.t_name='张三'
select s.* from student s, score sco where s.s_id=sco.s_id and sco.c_id in (select c.c_id from teacher t, course c where t.t_id=c.t_id and t.t_name='张三')
-- 8、查询没学过"张三"老师授课的同学的信息
select s.* from student s, score sco where s.s_id=sco.s_id and sco.c_id in (select c.c_id from teacher t, course c where t.t_id=c.t_id and t.t_name<>'张三')
select s.s_id from student s, score sco where s.s_id=sco.s_id and sco.c_id in (select c.c_id from teacher t, course c where t.t_id=c.t_id and t.t_name='张三')
select * from student where s_id not in (select s.s_id from student s, score sco where s.s_id=sco.s_id and sco.c_id in (select c.c_id from teacher t, course c where t.t_id=c.t_id and t.t_name='张三'))
-- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
select * from score where c_id='01';
select * from score where c_id='02'
(select s_id from score where c_id='01') intersect (select s_id from score where c_id='02')
SELECT s1.s_id from score s1, score s2 where s1.c_id='01' and s2.c_id='02' and s1.s_id=s2.s_id
select * from student where s_id in (SELECT s1.s_id from score s1, score s2 where s1.c_id='01' and s2.c_id='02' and s1.s_id=s2.s_id)
-- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
select * from score where c_id='01';
select s_id from score where c_id='02'
select stu.* from score s1, student stu where s1.c_id='01' and s1.s_id not in (select s_id from score where c_id='02') and s1.s_id=stu.s_id
-- 11、查询没有学全所有课程的同学的信息
select count(*) from course;
select s1.s_id from score s1 GROUP BY s1.s_id having count(*) < (select count(*) from course)
select * from student where s_id in (select s1.s_id from score s1 GROUP BY s1.s_id having count(*) < (select count(*) from course))
-- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
select c_id from score where s_id='01'
select DISTINCT s_id from score s1 where s1.c_id in (select c_id from score where s_id='01') and s1.s_id<>'01'
select * from student where s_id=any(select DISTINCT s_id from score s1 where s1.c_id in (select c_id from score where s_id='01') and s1.s_id<>'01')
-- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
select count(*) from score where s_id='01'
select s1.s_id from score s1 where s1.c_id in (select c_id from score where s_id='01') GROUP BY s1.s_id having count(*)=(select count(*) from score where s_id='01')
select * from student where s_id in (select s1.s_id from score s1 where s1.c_id in (select c_id from score where s_id='01') GROUP BY s1.s_id having count(*)=(select count(*) from score where s_id='01')) and s_id<>'01'
-- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
select c_id from course where t_id in (select t_id from teacher where t_name='张三')
select s_id from score where c_id not in (select c_id from course where t_id in (select t_id from teacher where t_name='张三'))
select s_name from student where s_id not in (select s_id from score where c_id not in (select c_id from course where t_id in (select t_id from teacher where t_name='张三')))
-- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select s_id from score where s_score < 60 GROUP BY s_id having count(*) >=2
select s_id, avg(s_score) avgscore from score where s_id in (select s_id from score where s_score < 60 GROUP BY s_id having count(*) >=2) GROUP BY s_id
SELECT stu.s_name, stu.s_id, a.avgscore from student stu, (select s_id, avg(s_score) avgscore from score where s_id in (select s_id from score where s_score < 60 GROUP BY s_id having count(*) >=2) GROUP BY s_id) a where stu.s_id=a.s_id
-- 16、检索"01"课程分数小于60,按分数降序排列的学生信息
select stu.* from score s1, student stu where s1.s_id=stu.s_id and s1.c_id='01' and s1.s_score<60 ORDER BY s1.s_score desc
-- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select sum(s_score), avg(s_score) from score GROUP BY s_id ORDER BY avg(s_score) desc
-- 18、查询各科成绩最高分、最低分和平均分
select max(s_score), min(s_score), avg(s_score) from score GROUP BY c_id;
-- 19、检索至少选修两门课程的学生学号
select s_id from score GROUP BY s_id having count(*) >=2
-- 20、查询学生的总成绩并进行排名
select sum(s_score) from score GROUP BY s_id ORDER BY sum(s_score) desc