时间:2021-07-01 10:21:17 帮助过:30人阅读
#1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
select a.* ,b.s_score as 01_score,c.s_score as 02_score from student a join score b on a.s_id=b.s_id and b.c_id=‘01‘ left join score c on a.s_id = c.s_id and c.c_id = ‘02‘ where b.s_score > c.s_score;
或者:
select a.*,b.s_score as 01_score,c.s_score as 02_score from student a,score b,score c where a.s_id=b.s_id and a.s_id=c.s_id and b.c_id=‘01‘ and c.c_id=‘02‘ and b.s_score>c.s_score
#查询平均成绩小于60分的同学和没有成绩的的学生编号和学生姓名和平均成绩
select a.s_id,a.s_name,round(avg(b.s_score),2) as avg_score
from student a left join score b on a.s_id = b.s_id group by a.s_id,a.s_name having avg_score < 60 or avg_score is null
#查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select a.s_id,a.s_name,round(avg(b.s_score),2) as avg_score from student a left join score b on a.s_id = b.s_id group by a.s_id,a.s_name having avg_score >=60
#查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select a.s_id,a.s_name,count(b.c_id) as total_course,sum(b.s_score) as total_score from student a left join score b on a.s_id = b.s_id group by a.s_id,a.s_name
#查询"李"姓老师的数量
select count(t.t_id) from teacher t where t.t_name like ‘李%‘
#查询学过"张三"老师授课的同学的信息
select a.* from student a inner join score b on a.s_id = b.s_id where b.c_id in( select c_id from course where t_id = ( select t_id from teacher where t_name = ‘张三‘ ) )
#查询没学过"张三"老师授课的同学的信息
select * from student c where c.s_id not in( select a.s_id from student a join score b on a.s_id=b.s_id where b.c_id in( select a.c_id from course a join teacher b on a.t_id = b.t_id where t_name =‘张三‘ ) );
#查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
select a.* from student a inner join score b on a.s_id = b.s_id inner join score c on a.s_id = c.s_id where b.c_id = ‘01‘ and c.c_id = ‘02‘;
或者
select a.* from student a,score b,score c where a.s_id = b.s_id and a.s_id = c.s_id and b.c_id=‘01‘ and c.c_id=‘02‘;
#查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
select * from student where s_id in (select s_id from score where c_id=‘01‘) and s_id not in (select s_id from score where c_id=‘02‘)
#查询没有学全所有课程的同学的信息
select a.* from student a left join score b on a.s_id = b.s_id group by a.s_id having count(b.s_id) < (select count(c_id) from course);
#查询至少有一门课与学号为"01"的同学所学相同的同学的信息
select a.* from student a inner join score b on a.s_id = b.s_id where b.c_id in ( select c_id from score where s_id =‘01‘ ) group by a.s_id;
#查询和"01"号的同学学习的课程完全相同的其他同学的信息
select a.* from student a inner join score b on a.s_id = b.s_id where a.s_id <> ‘01‘ group by b.s_id having group_concat(b.c_id ORDER BY c_id) = ( select group_concat(c_id ORDER BY c_id) from score where s_id = ‘01‘ );
#
转自:https://blog.csdn.net/fashion2014/article/details/78826299
sql练习
标签:birt none arc comment prim core join where efault