Hive sql 初级题 03

1、查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,按课程号降序排列

复制代码
select course_id,
       avg(score) avg
from score_info
group by course_id
order by avg, course_id desc;

2、统计参加考试人数大于等于15的学科

复制代码
select course_id,
       count(stu_id) as cnt
from score_info
group by course_id
having cnt >= 15;

3、查询学生的总成绩并按照总成绩降序排序

复制代码
select stu_id,
       sum(score) sum
from score_info
group by stu_id
order by sum desc;

4、显示学生的语文、数学、英语三科成绩,没有成绩的输出为0,按照学生的有效平均成绩降序显示

复制代码
select si.stu_id,
       sum(if(ci.course_name = '语文', score, 0)) `语文`,
       sum(if(ci.course_name = '数学', score, 0)) `数学`,
       sum(if(ci.course_name = '英语', score, 0)) `英语`,
       count(*)                                 `有效课程数`,
       avg(si.score)                            `平均成绩`
from score_info si
         join
     course_info ci
     on
         si.course_id = ci.course_id
group by si.stu_id
order by `平均成绩` desc;

5、查询一共参加三门课程且其中一门为语文课程的学生的id和姓名

复制代码
select
student_info.stu_id,
       stu_name
from student_info join
(select stu_id,
       count(*) cnt
from score_info
where stu_id in
      (select stu_id
       from score_info
       where course_id = "01")
group by stu_id
having cnt=3)t1 on t1.stu_id=student_info.stu_id;