Hive HQL SQL经典练习题50道

基于Hive1.1语法

1. 四张表结构

  1. 学生表
    student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
  2. 课程表
    course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
  3. 教师表
    teacher(t_id,t_name) –教师编号,教师姓名
  4. 成绩表
    score(s_id,c_id,s_score) –学生编号,课程编号,分数

2. 建表与创建文本

--学生表
create table student(s_id string,s_name string,s_birth string,s_sex string)
row format delimited fields terminated by '\t';
--课程表
create table course(c_id string,c_name string,t_id string)
row format delimited fields terminated by '\t';
--教师表
create table teacher(t_id string,t_name string)
row format delimited fields terminated by '\t';
--成绩表
create table score(s_id string,c_id string,s_score int)
row format delimited fields terminated by '\t';
--学生表数据
01	赵雷	1990-01-0102	钱电	1990-12-2103	孙风	1990-05-2004	李云	1990-08-0605	周梅	1991-12-0106	吴兰	1992-03-0107	郑竹	1989-07-0108	王菊	1990-01-20--课程表测试数据
01	语文	02
02	数学	01
03	英语	03
--教师表测试数据
01	张三
02	李四
03	王五
--成绩表测试数据
01	01	80
01	02	90
01	03	99
02	01	70
02	02	60
02	03	80
03	01	80
03	02	80
03	03	80
04	01	50
04	02	30
04	03	20
05	01	76
05	02	87
06	01	31
06	03	34
07	02	89
07	03	98

3. 将数据导入到Hive

-- 将文件上传到tmp目录下导入数据
load data local inpath '/tmp/student.txt' into table student;
load data local inpath '/tmp/course.txt' into table course;
load data local inpath '/tmp/score.txt' into table score;
load data local inpath '/tmp/teacher.txt' into table teacher;

4. 开启本地模式加快运行速度

set hive.exec.mode.local.auto=true

5. 答案(仅供参考)

1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数

select st.s_id, st.s_name, s1.s_score cou_01, s2.s_score cou_01
from student st
         join score s1 on st.s_id = s1.s_id and s1.c_id = '01'
         join score s2 on st.s_id = s2.s_id and s2.c_id = '02'
where s1.s_score > s2.s_score;

2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数

select st.s_id, st.s_name, s1.s_score cou_01, s2.s_score cou_01
from student st
         join score s1 on st.s_id = s1.s_id and s1.c_id = '01'
         join score s2 on st.s_id = s2.s_id and s2.c_id = '02'
where s1.s_score < s2.s_score;

3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

select st.s_id, st.s_name, avg(sc.s_score) avgScore
from student st
         left join score sc on st.s_id = sc.s_id
group by st.s_id, st.s_name
having avgScore > 60;

4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩 – (包括有成绩的和无成绩的)

select st.s_id, st.s_name, avg(sc.s_score) avgScore
from student st
         left join score sc on st.s_id = sc.s_id
group by st.s_id, st.s_name
having avgScore < 60
    or avgScore is null;

5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

select st.s_id, st.s_name, count(sc.c_id) sumCourse, sum(sc.s_score) sumScore
from student st
         left join score sc on st.s_id = sc.s_id
group by st.s_id, st.s_name;

6、查询"李"姓老师的数量

select count(*)
from teacher
where t_name like '李%';

7、查询学过"张三"老师授课的同学的信息

select *
from student stu
         join (
    select s_id
    from teacher t
             join course c on t.t_name = '张三' and t.t_id = c.t_id
             JOIN score s on c.c_id = s.c_id
) t1 on stu.s_id = t1.s_id;

8、查询没学过"张三"老师授课的同学的信息

select stu.*
from student stu
         left join (
    select s_id
    from teacher t
             join course c on t.t_name = '张三' and t.t_id = c.t_id
             join score s on c.c_id = s.c_id
) t1 on stu.s_id = t1.s_id
where t1.s_id is null;

9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

select st.*
from student st
         join score s1 on st.s_id = s1.s_id and s1.c_id = '01'
         join score s2 on st.s_id = s2.s_id and s2.c_id = '02';

10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

select st.*
from student st
         join (select t1.s_id
               from (select s_id, c_id from score where c_id = '01') t1
                        left join (select s_id, c_id from score where c_id = '02') t2
                                  on t1.s_id = t2.s_id
               where t2.c_id is null) t3 on st.s_id = t3.s_id;
-----------------------------------------------------------------------
select st.*
from student st
         join score s1 on st.s_id = s1.s_id and s1.c_id = '01'
where not exists (select 1 from score s2 where s2.c_id = '02' and st.s_id = s2.s_id);

11、查询没有学全所有课程的同学的信息

select st.*
from student st
         join (select count(c_id) n1 from course) t1
         left join (select s_id, count(c_id) n2 from score group by s_id) t2
                   on st.s_id = t2.s_id and t1.n1 = t2.n2
where t2.s_id is null;

12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息

select distinct st.*
from student st
         join score sc1 on sc1.s_id = '01'
         join score sc2 on sc1.c_id = sc2.c_id and st.s_id = sc2.s_id
where st.s_id <> '01';

13、查询和"01"号的同学学习的课程完全相同的其他同学的信息

select st.s_id, st.s_name, st.s_birth, st.s_sex
from student st
         join score sc on st.s_id != '01' and st.s_id = sc.s_id
         join (select collect_set(s1.c_id) coll, count(s1.s_id) num
               from score s1
               where s1.s_id = '01') t1
where array_contains(t1.coll, sc.c_id)
group by st.s_id, st.s_name, st.s_birth, st.s_sex, t1.num
having t1.num = count(st.s_id);
---------------------------------------------------------------
select st.*
from student st
         join (select s1.s_id, concat_ws(',', sort_array(collect_set(s1.c_id))) allCid
               from score s1
               group by s1.s_id
) t1 on st.s_id != '01' and st.s_id = t1.s_id
         join (select concat_ws(',', sort_array(collect_set(s2.c_id))) firstCid
               from score s2
               where s2.s_id = '01') t2 on t1.allCid = t2.firstCid;

14、查询没学过"张三"老师讲授的任一门课程的学生姓名 | 与8题相同另一种思路

select *
from student st
where not exists (
    select 1
    from teacher t
             join course c on t.t_name = '张三' and t.t_id = c.t_id
             join score s on c.c_id = s.c_id
    where st.s_id = s.s_id);

15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

select st.s_id, st.s_name, round(t1.avgScore, 2)
from student st
         join (select s1.s_id, avg(s1.s_score) avgScore
               from score s1
               where s1.s_score < 60
               group by s1.s_id
               having count(*) >= 2) t1 on st.s_id = t1.s_id;

16、检索"01"课程分数小于60,按分数降序排列的学生信息

select st.*, sc.s_score
from student st
         join score sc on sc.c_id = '01' and sc.s_score < 60 and st.s_id = sc.s_id
order by sc.s_score desc;

17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

select st.s_id,
       st.s_name,
       max(if(c.c_name = '语文', sc.s_score, 0)) chinese,
       max(if(c.c_name = '数学', sc.s_score, 0)) math,
       max(if(c.c_name = '英语', sc.s_score, 0)) english,
       round(nvl(avg(sc.s_score), 0), 2)       avgScore
FROM student st
         left join score sc on st.s_id = sc.s_id
         left join course c on sc.c_id = c.c_id
group by st.s_id, st.s_name
order by avgScore desc;

18.查询各科成绩最高分、最低分和平均分: 以如下形式显示:

课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率

select co.c_id,
       co.c_name,
       max(sc.s_score)                                                max_score,
       min(sc.s_score)                                                min_score,
       round(avg(sc.s_score), 2)                                      avg_score,
       sum(if(sc.s_score >= 60, 1, 0)) / count(*)                     pass_rate,
       sum(if(sc.s_score >= 70 and sc.s_score < 80, 1, 0)) / count(*) medium_rate,
       sum(if(sc.s_score >= 80 and sc.s_score < 90, 1, 0)) / count(*) good_rate,
       sum(if(sc.s_score >= 90, 1, 0)) / count(*)                     excellence_rate
from score sc
         join course co on sc.c_id = co.c_id
group by co.c_id, co.c_name;

19、按各科成绩进行排序,并显示排名:– row_number() over()分组排序功能(mysql没有该方法)

select *, row_number() over (partition by sc.c_id order by sc.s_score desc)
from score sc;

20、查询学生的总成绩并进行排名

select st.s_id, sum(sc.s_score) sumScore, row_number() over (order by sum(sc.s_score) desc ) rk
from student st
         join score sc
              on st.s_id = sc.s_id
group by st.s_id
order by sumScore desc;

21、查询不同老师所教不同课程平均分从高到低显示

select t.t_id, c.c_name, round(avg(sc.s_score), 2) avgScore
from teacher t
         join course c on t.t_id = c.t_id
         join score sc on c.c_id = sc.c_id
group by t.t_id, c.c_name
order by avgScore desc;

22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩

select st.s_id, st.s_name, t1.c_id, t1.s_score, t1.rk
from student st
         join (select sc.s_id,
                      sc.c_id,
                      sc.s_score,
                      row_number() over (partition by sc.c_id order by sc.s_score desc) rk
               from score sc) t1
              on st.s_id = t1.s_id
where t1.rk = 2
   or t1.rk = 3;

23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比

select c.c_id,
       c.c_name,
       sum(if(s.s_score <= 100 and s.s_score > 85, 1, 0))                      100_85,
       round(sum(if(s.s_score <= 100 and s.s_score > 85, 1, 0)) / count(*), 2) 100_85percentage,
       sum(if(s.s_score <= 85 and s.s_score > 70, 1, 0))                       70_85,
       round(sum(if(s.s_score <= 85 and s.s_score > 70, 1, 0)) / count(*), 2)  70_85percentage,
       sum(if(s.s_score <= 70 and s.s_score > 60, 1, 0))                       60_70,
       round(sum(if(s.s_score <= 70 and s.s_score > 60, 1, 0)) / count(*), 2)  60_70percentage,
       sum(if(s.s_score <= 60 and s.s_score >= 0, 1, 0))                       0_60,
       round(sum(if(s.s_score <= 60 and s.s_score >= 0, 1, 0)) / count(*), 2)  0_60percentage
from score s
         join course c on s.c_id = c.c_id
group by c.c_id, c.c_name;

24、查询学生平均成绩及其名次

select t1.s_id,
       t1.s_name,
       t1.avg_score,
       row_number() over (order by t1.avg_score desc) rk
from (select st.s_id, st.s_name, round(nvl(avg(sc.s_score), 0), 2) avg_score
      from student st
               left join score sc on st.s_id = sc.s_id
      group by st.s_id, st.s_name) t1;

25、查询各科成绩前三名的记录

select t1.*
from (select sc.s_id,
             sc.c_id,
             sc.s_score,
             row_number() over (partition by sc.c_id order by sc.s_score desc) rk
      from score sc) t1
where t1.rk <= 3;

26、查询每门课程被选修的学生数

select sc.c_id, count(sc.s_id)
from score sc
group by sc.c_id;

27、查询出只有两门课程的全部学生的学号和姓名

select sc.s_id, st.s_name
from student st
         join score sc on st.s_id = sc.s_id
group by sc.s_id, st.s_name
having count(sc.c_id) = 2;

28、查询男生、女生人数

select s_sex, count(*) num
from student
group by s_sex;

29、查询名字中含有"风"字的学生信息

select *
from student
where s_name like '%风%';

30、查询同名同性学生名单,并统计同名人数

select s_name, s_sex, count(*)
from student
group by s_name, s_sex
having count(*) > 1;

31、查询1990年出生的学生名单

select *
from student
where year(s_birth) = '1990';

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

select c.c_id, c.c_name, round(avg(s.s_score), 2) avgScore
from score s
         join course c on s.c_id = c.c_id
group by c.c_id, c_name
order by avgScore desc, c.c_id;

33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩

select st.s_id, st.s_name, avg(s.s_score) avgScore
from student st
         join score s on st.s_id = s.s_id
group by st.s_id, st.s_name
having avgScore >= 85;

34、查询课程名称为"数学",且分数低于60的学生姓名和分数

select st.s_name, c.c_name, s_score
from student st
         join score s on s.s_score < 60 and st.s_id = s.s_id
         join course c on c.c_name = '数学' and s.c_id = c.c_id;

35、查询所有学生的课程及分数情况

select *
from student st
         left join score s on st.s_id = s.s_id
         left join course c on s.c_id = c.c_id;

36、查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数

select st.s_name, c.c_name, s.s_score
from student st
         join score s on st.s_id = s.s_id
         join course c on s.c_id = c.c_id
where s.s_score > 70;

37、查询课程不及格的学生

select st.s_name, c.c_name, s.s_score
from student st
         join score s on st.s_id = s.s_id
         join course c on s.c_id = c.c_id
where s.s_score < 60;

38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名

select st.s_id, st.s_name, s.s_score
from student st
         join score s on s.c_id = '01' and s.s_score > 80 and st.s_id = s.s_id;

39、求每门课程的学生人数

select c.c_id, c.c_name, count(*) num_student
from score s
         join course c on s.c_id = c.c_id
group by c.c_id, c_name;

40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩

select st.s_id, st.s_name, c.c_name, s.s_score
from teacher t
         join course c on t.t_name = '张三' and t.t_id = c.t_id
         join score s on c.c_id = s.c_id
         join student st on s.s_id = st.s_id
order by s.s_score desc
limit 1;

41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

SELECT distinct s1.s_id, s1.c_id, s1.s_score
FROM score s1
         JOIN score s2 ON s1.s_id = s2.s_id and s1.s_score = s2.s_score
where s1.c_id != s2.c_id;

42、查询每门课程成绩最好的前三名

select t1.*
from (select st.s_id,
             st.s_name,
             s.c_id,
             s.s_score,
             row_number() over (partition by s.c_id order by s.s_score desc) rk
      from student st
               join score s on st.s_id = s.s_id) t1
where t1.rk <= 3;

43、统计每门课程的学生选修人数(超过5人的课程才统计):

select c.c_id, c_name, count(*) num_student
from score s
         join course c on s.c_id = c.c_id
group by c.c_id, c_name
having num_student > 5;

44、检索至少选修两门课程的学生学号

select s_id, count(*) num_course
from score
group by s_id
having num_course >= 2;

45、查询选修了全部课程的学生信息

select st.*
from student st
         join (select s_id, count(*) stu_num
               from score
               group by s_id) t1 on st.s_id = t1.s_id
         join (select count(*) all_num
               from course) t2 on t1.stu_num = t2.all_num;

46、查询各学生的年龄(周岁)

select *, floor(datediff(current_date, s_birth) / 365.25) age
from student;

47、查询本周过生日的学生(这题与下一题表述不明确,不必太纠结)

select *
from student
where weekofyear(current_date()) = weekofyear(s_birth);

48、查询下周过生日的学生

select *
from student
where weekofyear(date_add(current_date(), 7)) = weekofyear(s_birth);

49、查询本月过生日的学生

select *
from student
where month(s_birth) = month(current_date);

50、查询12月份过生日的学生

select *
from student
where month(s_birth) = 12;

你可能感兴趣的:(Hive,hive,sql)