【Hive】Hive练习题50道

数据展示

student表
【Hive】Hive练习题50道_第1张图片
score表
【Hive】Hive练习题50道_第2张图片
teacher表
【Hive】Hive练习题50道_第3张图片
course表
【Hive】Hive练习题50道_第4张图片

在hive中建表导入数据

首先要先在hdfs上为每个数据建一个文件名相同的文件夹,以上的4张表都是txt格式的,放入hdfs相对应的文件夹后,使用以下语句建表(因为数据量不大,就直接建内部表)

create table if not exists student(
id int,
name string,
birthday string,
sex string
)
row format delimited fields terminated by '\t'
stored as textfile
location '/data/myschool/student';

create table if not exists teacher(
tid int,
tname string
)
row format delimited fields terminated by '\t'
stored as textfile
location '/data/myschool/teacher';

create table if not exists score(
sid int,
cid int,
scores int
)
row format delimited fields terminated by '\t'
stored as textfile
location '/data/myschool/score';

create table if not exists course(
cid int,
cname string,
tid int
)
row format delimited fields terminated by '\t'
stored as textfile
location '/data/myschool/course';

题目

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

select stu.*,sco1.scores 01scores,sco2.scores 02scores from 
student stu join score sco1 
on stu.id=sco1.sid and sco1.cid=1
left join score sco2 
on stu.id=sco2.sid and sco2.cid=2
where sco1.scores>sco2.scores;

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

select stu.*,sco1.scores 01scores,sco2.scores 02scores from 
student stu join score sco1 
on stu.id=sco1.sid and sco1.cid=1
left join score sco2 
on stu.id=sco2.sid and sco2.cid=2
where sco1.scores<sco2.scores;

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

select stu.id,stu.name,avg(sco.scores)
from student stu join score sco
on stu.id=sco.sid
group by stu.id,stu.name
having avg(sco.scores)>60;

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

select stu.id,stu.name,round(avg(sco.scores),2) as avg_scores
from student stu join score sco
on stu.id=sco.sid
group by stu.id,stu.name
having avg(sco.scores)<60
union all
select stu1.id,stu1.name,0 as avg_scores
from student stu1
where stu1.id not in
(select distinct sid from score);

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

select stu.id,stu.name,count(cid),sum(scores)
from student stu left join score sco
on stu.id=sco.sid
group by stu.id,stu.name;

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

select count(tid) as num,'姓李的老师' as teal
from teacher 
where tname like '李%';

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

select stu.* 
from student stu join score sco on stu.id=sco.sid
join course cor on sco.cid=cor.cid
join teacher tea on tea.tid=cor.tid
where tea.tname='张三';

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

select s.* from student s 
where s.id not in
(select stu.id 
from student stu join score sco on stu.id=sco.sid
join course cor on sco.cid=cor.cid
join teacher tea on tea.tid=cor.tid
where tea.tname='张三');

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

select stu.*
from student stu
join 
(select sid as tmpid from score
where cid=1
union all
select sid as tmpid from score
where cid=2) ss on stu.id=ss.tmpid
group by stu.id,stu.name,stu.birthday,stu.sex,ss.tmpid
having count(ss.tmpid)=2;

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

select stu.* 
from student stu 
join (select sid from score where cid=1) sco1
on stu.id=sco1.sid
left join (select sid from score where cid=2) sco2
on stu.id=sco2.sid
where sco2.sid is null;

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

select student.* from student
left join(
select sid
from score
group by sid
having count(cid)=3)tmp
on student.id=tmp.sid
where tmp.sid is null;

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

select stu.* from student stu
join (select cid from score where sid=1) tmp1
join (select sid,cid from score) tmp2
on tmp1.cid=tmp2.cid and stu.id=tmp2.sid
where stu.id not in (1)
group by stu.id,name,birthday,sex;

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

select stu.*,count(tmp2.cid) from student stu
join (select cid from score where sid=1) tmp1
join (select sid,cid from score) tmp2
on tmp1.cid=tmp2.cid and stu.id=tmp2.sid
where stu.id not in (1)
group by stu.id,name,birthday,sex
having count(tmp2.cid) in (select count(cid) from score where sid=1);

14、查询没学过"张三"老师讲授的任一门课程的学生姓名

select stu.id,stu.name from student stu
left join (select sid,cid from score) sco
left join (select cid,tid from course) cor
left join (select tid from teacher where tname='张三') tea
on stu.id=sco.sid and sco.cid=cor.cid and tea.tid=cor.tid 
group by stu.id,name
having count(tea.tid)=0;

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

select stu.*,tmp.savg from student stu 
join (select sid,count(cid) noc,round(avg(scores),1) savg 
from score where scores<60 group by sid having noc>=2) tmp
on tmp.sid=stu.id;

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

select stu.*,tmp.scores from student stu join
(select sid,scores from score where cid=1 and scores<60) tmp
on stu.id=tmp.sid
order by tmp.scores desc;

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

select a.sid,tmp1.scores as c1,tmp2.scores as c2,tmp3.scores as c3,
    round(avg (a.scores),2) as avgScore
from score a
left join (select sid,scores  from score s1 where  cid='01')tmp1 on  tmp1.sid=a.sid
left join (select sid,scores  from score s2 where  cid='02')tmp2 on  tmp2.sid=a.sid
left join (select sid,scores  from score s3 where  cid='03')tmp3 on  tmp3.sid=a.sid
group by a.sid,tmp1.scores,tmp2.scores,tmp3.scores order by avgScore desc;

18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率

select course.cid,course.cname,tmp.maxScore,tmp.minScore,tmp.avgScore,tmp.passRate,tmp.moderate,tmp.goodRate,tmp.excellentRates from course
join(select 
    cid,
    max(scores) as maxScore,
    min(scores) as minScore,
    round(avg(scores),2) avgScore,
    round(sum(case when scores>=60 then 1 else 0 end)/count(cid),2)passRate,
    round(sum(case when scores>=60 and scores<70 then 1 else 0 end)/count(cid),2) moderate,
    round(sum(case when scores>=70 and scores<80 then 1 else 0 end)/count(cid),2) goodRate,
    round(sum(case when scores>=80 and scores<90 then 1 else 0 end)/count(cid),2) excellentRates
from score group by cid) tmp on tmp.cid=course.cid;

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

select cid,sid,scores,row_number() over(partition by cid order by scores desc) 
from score;

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

select score.sid,student.name,sum(scores) sum_sco,row_number() over(order by sum(scores) desc) no
from score join student on score.sid=student.id
group by score.sid,student.name;

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

select score.cid,round(avg(scores),2) avg_scores,course.tid 
from score join
course on score.cid=course.cid
group by score.cid,course.tid
order by avg_scores desc;

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

select tmp.cid,stu.*,tmp.scores,tmp.cno from
student stu join
(select cid,sid,scores,row_number() over(partition by cid order by scores desc) cno
from score) tmp
on stu.id=tmp.sid
where tmp.cno between 2 and 3;

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

select 
score.cid,
course.cname,
round(sum(case when score.scores>=85 and score.scores<=100 then 1 else 0 end)/count(score.scores),2) as 100and85,
round(sum(case when score.scores>=70 and score.scores<85 then 1 else 0 end)/count(score.scores),2) as 85and70,
round(sum(case when score.scores>=60 and score.scores<70 then 1 else 0 end)/count(score.scores),2) as 70and60,
round(sum(case when score.scores>=0 and score.scores<60 then 1 else 0 end)/count(score.scores),2) as 60and0
from score left join course
on score.cid = course.cid
group by score.cid,course.cname;

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

select sid,round(avg(scores),2) as avgs,row_number() over(order by avg(scores) desc)
from score
group by sid;

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

select tmp.cid,stu.*,tmp.scores,tmp.cno from
student stu join
(select cid,sid,scores,row_number() over(partition by cid order by scores desc) cno
from score) tmp
on stu.id=tmp.sid
where tmp.cno<=3;

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

select cid,count(scores) as cnum
from score
group by cid;

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

select sid,count(cid) as cnum
from score
group by sid
having count(cid)=2;

28、查询男生、女生人数

select sex,count(1) as pnum
from student
group by sex;

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

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

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

select name,sex,count(id)
from student
group by name,sex;

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

select * 
from student
where year(birthday)=1990;

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

select cid,round(avg(scores),2) as avgs,row_number() over(order by round(avg(scores),2) desc,cid asc)
from score
group by cid;

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

select stu.id,stu.name,avg(scores) as avgs
from student stu join
score sco on stu.id=sco.sid
group by stu.id,stu.name
having avg(scores)>85;

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

select stu.name,sco.scores
from student stu
join score sco 
join course cor
on stu.id=sco.sid and sco.cid=cor.cid
where cor.cname='数学' and sco.scores<60;

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

select stu.id,tmp.chinese,tmp.math,tmp.english
from student stu
left join
(select sco.sid id,
        sum(case cor.cname when '语文' then sco.scores else 0 end) as chinese, 
        sum(case cor.cname when '数学' then sco.scores else 0 end) as math,
        sum(case cor.cname when '英语' then sco.scores else 0 end) as english
 from score sco
 join course cor on sco.cid=cor.cid
 group by sco.sid
) tmp on stu.id=tmp.id;

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

select stu.name,cor.cname,sco.scores
from score sco
left join student stu on sco.sid=stu.id
join course cor on sco.cid=cor.cid
where sco.scores>70;

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

select sid
from score
where scores<60
group by sid;

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

select sco.sid,stu.name
from score sco
join student stu 
on sco.sid=stu.id
where cid=1 and scores>=80;

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

select cid,count(sid)
from score
group by cid;

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

select stu.*,sco.cid,max(sco.scores) max_score
from score sco
left join student stu
on stu.id=sco.sid
join course cor
on sco.cid=cor.cid
join teacher tea
on tea.tid=cor.tid
where tea.tname='张三'
group by sco.cid,stu.id,stu.name,stu.birthday,stu.sex
limit 1;

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

select s1.sid,s1.cid,s1.scores
from score s1,score s2
where s1.cid<>s2.cid and s1.scores=s2.scores;

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

select tmp.cid,stu.*,tmp.scores,tmp.cno from
student stu join
(select cid,sid,scores,row_number() over(partition by cid order by scores desc) cno
from score) tmp
on stu.id=tmp.sid
where tmp.cno<=3;

43、统计每门课程的学生选修人数(超过5人的课程才统计):
–要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

select cid,count(sid) as num
from score
group by cid
having num>=5
order by num desc,cid asc;

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

select sid
from score
group by sid
having count(cid)>=2;

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

select stu.*
from student stu
join 
(select sid,count(cid) cnum from score group by sid) tmp
on stu.id=tmp.sid
where tmp.cnum=3;

46、查询各学生的年龄(周岁):
–按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一

with tmp as
(select id,year(current_date())-year(birthday) as tage
from student)
select stu.id,sum(case month(current_date())>month(stu.birthday) when true then tmp.tage-1 else tmp.tage end) s_age 
from student stu
join tmp
on stu.id=tmp.id
group by stu.id;

47、查询本周过生日的学生:

select *
from student 
where weekofyear(concat(year(current_date()),'-',date_format(birthday,'MM-dd')))=
      weekofyear(current_date())

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

select *
from student 
where weekofyear(concat(year(current_date()),'-',date_format(birthday,'MM-dd')))=
      weekofyear(current_date())+1;

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

select *
from student 
where month(birthday)=month(current_date())

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

select * from student where month(birthday)=12

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