1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
select s.*,s1.s_score,s2.s_score from student s, score s1,score s2 where s.s_id=s1.s_id and s1.s_id=s2.s_id and s1.c_id=‘01’ and s2.c_id=‘02’ and s1.s_score>s2.s_score;
3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select * from student t1,(select score.s_id as id,avg(score.s_score) as avg_s from score group by score.s_id) as t2 where t1.s_id=t2.id and t2.avg_s>=60;
分析,首先,要想求平分成绩,那么必须要group by一次,并且是按照学生的学号进行分组。题目需要知道平均成绩,和学生信息等。score表可以提供平均成绩和学号信息,因此,第一次group by操作 产生学生学号,和平均成绩 as t1
之后,我们将这个表与学生信息表进行关联
5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select t1.s_id,t1.s_name,t2.sumc,t2.sums from student t1,(select t3.s_id as s_id ,sum(t3.s_score) as sums,count(*) as sumc from score t3 group by t3.s_id) as t2 where t1.s_id=t2.s_id;
6、查询"李"姓老师的数量
select count(*) from teacher where teacher.s_name like ‘李%’;
7、查询学过"张三"老师授课的同学的信息
select t1.* from student t1,teacher t2,course t3,score t4 where t2.s_name=‘张三’
and t2.t_id = t3.t_id
and t3.c_id = t4.c_id
and t1.s_id = t4.s_id;
8、查询没学过"张三"老师授课的同学的信息
select * from student t5 where t5.s_id not in (
select t1.s_id from student t1,teacher t2,course t3,score t4 where t2.s_name=‘张三’
and t2.t_id = t3.t_id
and t3.c_id = t4.c_id
and t1.s_id = t4.s_id);
9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息全
全链接查询
select * from student s0,score s1,score s2 where s0.s_id=s1.s_id and s1.s_id=s2.s_id and s1.c_id=‘01’ and s2.c_id=‘02’;
优化查询
select * from student t0,(select * from score t1 where t1.c_id=‘01’ and t1.s_id in (select t2.s_id from score t2 where t2.c_id=‘02’)) as t3 where t0.s_id=t3.s_id;
11、查询没有学全所有课程的同学的信息
select * from student where student.s_id in(select s.s_id from score s group by s.s_id having count()=(select count() from course));