hive练习(转载)

数据准备
student

01	赵雷	1990-01-01	男
02	钱电	1990-12-21	男
03	孙风	1990-05-20	男
04	李云	1990-08-06	男
05	周梅	1991-12-01	女
06	吴兰	1992-03-01	女
07	郑竹	1989-07-01	女
08	王菊	1990-01-20	女

course

01	语文	02
02	数学	01
03	英语	03

teacher

01	张三
02	李四
03	王五

score

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

建表语句
create table if not exists student(
s_id string,
s_name string,
s_birth string,
s_sex string
)
row format delimited
fields terminated by ‘\t’
stored as textfile

create table if not exists course(
c_id string,
c_name string,
t_id string
)
row format delimited
fields terminated by ‘\t’
stored as textfile

create table if not exists teacher(
t_id string,
t_name string
)
row format delimited
fields terminated by ‘\t’
stored as textfile

create table if not exists score(
s_id string,
c_id string,
s_score int
)
row format delimited
fields terminated by ‘\t’
stored as textfile

导入数据
vi /data/student.txt
将上面的学生数据复制粘贴(下同)
vi /data/course.txt
vi /data/score.txt
vi /data/teacher.txt

加载本地数据到hive表中
load data local inpath ‘vi /data/student.txt’ into table student;
load data local inpath ‘vi /data/course.txt’ into table course;
load data local inpath ‘vi /data/score.txt’ into table score;
load data local inpath ‘vi /data/teacher.txt’ into table teacher;

题目
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;

2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数:和题一类似

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

– ROUND(x,d) 保留d位小数,当d为负数时表示该位以之后位全部为0,ROUND(1123.56,-2)=1100
SELECT *
FROM (
	SELECT a.s_id, a.s_name
		, round(AVG(b.s_score), 2) AS avg_score
	FROM student a
		JOIN score b ON a.s_id = b.s_id
	GROUP BY a.s_id, a.s_name
) t
WHERE t.avg_score >= 60



SELECT a.s_id, a.s_name, AVG(b.s_score) AS avg_score
FROM student a
	JOIN score b ON a.s_id = b.s_id
GROUP BY a.s_id, a.s_name
HAVING avg_score >= 60

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

SELECT *
FROM (
	SELECT a.s_id, a.s_name
		, round(AVG(b.s_score), 2) AS avg_score
	FROM student a
		JOIN score b ON a.s_id = b.s_id
	GROUP BY a.s_id, a.s_name
) t
WHERE t.avg_score < 60
UNION ALL
SELECT c.s_id, c.s_name, NULL AS avg_score
FROM student c
	LEFT JOIN score d ON c.s_id = d.s_id
WHERE d.s_score IS NULL

--round() 四舍五入截取

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

SELECT a.s_id, a.s_name, COUNT(b.c_id) AS num_subject
	, SUM(b.s_score) AS total_score
FROM student a
	JOIN score b ON a.s_id = b.s_id
GROUP BY a.s_id, a.s_name;

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

SELECT COUNT(*)
FROM teacher a
WHERE a.t_name LIKE “李%”;

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

SELECT DISTINCT a.*
FROM student a
	JOIN score b ON a.s_id = b.s_id
	JOIN course c ON b.c_id = c.c_id
	JOIN teacher d
	ON c.t_id = d.t_id	AND d.t_name = ‘张三’

8、查询没学过"张三"老师授课的同学的信息:(也就是分数没有)

SELECT a.s_id, a.s_name, a.s_birth, a.s_sex
FROM student a
	JOIN teacher b ON b.t_name = ‘张三’
	JOIN course c ON b.t_id = c.t_id
	LEFT JOIN score d
	ON d.s_id = a.s_id
		AND d.c_id = c.c_id
GROUP BY a.s_id, a.s_name, a.s_birth, a.s_sex
HAVING SUM(CASE 
	WHEN d.s_score IS NULL THEN 0
	ELSE 1
END) = 0

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

SELECT a.s_id, a.s_name, a.s_birth, a.s_sex
FROM student a
	JOIN score b
	ON a.s_id = b.s_id
		AND b.c_id = ‘01’
WHERE EXISTS (
	SELECT 1
	FROM score c
	WHERE a.s_id = c.s_id
		AND c.c_id = ‘02’
)


SELECT a.*
FROM student a
JOIN score b ON a.s_id=b.s_id AND b.c_id=‘01’
JOIN score c ON a.s_id=c.s_id AND c.c_id=‘02’;


SELECT a.s_id, a.s_name, a.s_birth, a.s_sex
FROM student a
	JOIN score b ON a.s_id = b.s_id
WHERE b.c_id IN (‘01’, ‘02’)
GROUP BY a.s_id, a.s_name, a.s_birth, a.s_sex
HAVING COUNT(1) = 2

10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息:(和没上过老师课的类似)

select
a.s_id,a.s_name,a.s_birth,a.s_sex
from student a
join score b on a.s_id=b.s_id and b.c_id=‘01’
where not exists(select 1 from score c where a.s_id=c.s_id and c.c_id=‘02’)

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

select distinct
a.*
from student a
join score b
left join score c on a.s_id=c.s_id and b.c_id=c.c_id
where c.s_score is null

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

select distinct
a.*
from student a
join score b on b.s_id=‘01’
join score c on b.c_id=c.c_id and a.s_id=c.s_id
where a.s_id<>‘01’

select distinct
a.*
from student a
join score b on b.s_id=‘01’
where exists(select 1 from score c where b.c_id=c.c_id and a.s_id=c.s_id) and a.s_id<>‘01’

SELECT a.*
FROM student a
JOIN score b ON a.s_id=b.s_id
WHERE b.s_id!=‘01’ AND b.c_id IN (SELECT c_id FROM score WHERE s_id=‘01’)
GROUP BY a.s_id,a.s_name,a.s_birth,a.s_sex;

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

select p.s_id,p.s_name,p.s_birth,p.s_sex from(
SELECT a.s_id as s_id,a.s_name as s_name,a.s_birth as s_birth,a.s_sex as s_sex,count(b.c_id) as num_course
FROM student a
JOIN score b ON a.s_id=b.s_id
WHERE b.s_id!=‘01’ AND b.c_id IN (SELECT c_id FROM score WHERE s_id=‘01’)
GROUP BY a.s_id,a.s_name,a.s_birth,a.s_sex
) p
right join (SELECT COUNT(c_id) as 01_num_course FROM score WHERE s_id=‘01’) q on p.num_course=q.01_num_course

select s.s_name from student s join score sc on s.s_id=sc.s_id
join(select collect_set(c_id) as sub,count(c_id) as num from score where s_id=‘01’ ) sc2
where array_contains(sc2.sub,sc.c_id)
group by s.s_id,s.s_name,s.s_birth,s.s_sex,sc2.num
having count(sc.c_id)=sc2.num

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

SELECT a.s_id, a.s_name, a.s_birth, a.s_sex
FROM student a
	JOIN teacher b ON b.t_name = ‘张三’
	JOIN course c ON b.t_id = c.t_id
	LEFT JOIN score d
	ON d.s_id = a.s_id
		AND d.c_id = c.c_id
GROUP BY a.s_id, a.s_name, a.s_birth, a.s_sex
HAVING SUM(CASE WHEN d.s_score IS NULL THEN 0 ELSE 1 END) = 0


SELECT a.*
FROM student a
WHERE NOT EXISTS (
	SELECT 1
	FROM score b, course c, teacher d
	WHERE (a.s_id = b.s_id
		AND b.c_id = c.c_id
		AND c.t_id = d.t_id
		AND d.t_name = ‘张三’)
)


SELECT *
FROM student a
WHERE a.s_id NOT IN (
	SELECT w.s_id
	FROM (
		SELECT DISTINCT a.*
		FROM student a
			JOIN score b ON a.s_id = b.s_id
			RIGHT JOIN (
				SELECT c_id
				FROM course a
					JOIN (
						SELECT t_id
						FROM teacher
						WHERE t_name = ‘张三’
					) b
					ON a.t_id = b.t_id
			) t
			ON b.c_id = t.c_id
	) w
)

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

SELECT a.s_id, a.s_name
	, ROUND(AVG(b.s_score), 2)
FROM student a
	JOIN score b
	ON a.s_id = b.s_id
		AND b.s_score < 60
GROUP BY a.s_id, a.s_name
HAVING COUNT(1) >= 2;

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

SELECT a.*, b.c_id, b.s_score
FROM student a
	JOIN score b
	ON a.s_id = b.s_id
		AND b.s_score < 60
WHERE b.c_id = ‘01’
ORDER BY b.s_score DESC;

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

SELECT a.s_id, b.s_score AS 01_score, c.s_score AS 02_score, d.s_score AS 03_score
	, ROUND((b.s_score + c.s_score + d.s_score) / 3, 2) AS avg_score
FROM score a
	JOIN score b
	ON a.s_id = b.s_id
		AND b.c_id = ‘01’
	JOIN score c
	ON a.s_id = c.s_id
		AND c.c_id = ‘02’
	JOIN score d
	ON a.s_id = d.s_id
		AND d.c_id = ‘03’
GROUP BY a.s_id, b.s_score, c.s_score, d.s_score
ORDER BY ROUND((b.s_score + c.s_score + d.s_score) / 3, 2) DESC;

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

SELECT a.c_id, b.c_name, MAX(a.s_score) AS max_score
	, MIN(a.s_score) AS min_score
	, ROUND(AVG(a.s_score), 2) AS avg_score
	, round(COUNT(if(a.s_score >= 60, a.s_score, NULL)) / COUNT(a.s_score) * 100, 2) AS jige
	, round(COUNT(if(a.s_score >= 70
		AND a.s_score < 80, a.s_score, NULL)) / COUNT(a.s_score) * 100, 2) AS zd
	, round(COUNT(if(a.s_score >= 80
		AND a.s_score < 90, a.s_score, NULL)) / COUNT(a.s_score) * 100, 2) AS yl
	, round(COUNT(if(a.s_score >= 90, a.s_score, NULL)) / COUNT(a.s_score) * 100, 2) AS yx
FROM score a
	JOIN course b ON a.c_id = b.c_id
GROUP BY a.c_id, b.c_name;

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

select *,
row_number() over(distribute by c_id sort by s_score desc) as rm
from score

--row_number() OVER (PARTITION BY COL1 ORDERBY COL2)
表示根据COL1分组,在分组内部根据COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(该编号在组内是连续并且唯一的)

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

select s_id,
sum(s_score) as total_score,
row_number() over(sort by sum(s_score) desc) as rm
from score
group by s_id

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

SELECT a.c_id, b.c_name, c.t_name
	, round(AVG(a.s_score), 2) AS avg_score
FROM score a
	JOIN course b ON a.c_id = b.c_id
	JOIN teacher c ON b.t_id = c.t_id
GROUP BY a.c_id, b.c_name, c.t_name
ORDER BY avg_score DESC

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

select b.*,a.rm,a.s_score,a.c_id 
from (
select *,row_number() over(distribute by c_id sort by s_score desc) as rm
from score
) a
join student b on b.s_id=a.s_id
where a.rm=2 or a.rm=3

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

SELECT a.c_id, b.c_name
	, COUNT(if(a.s_score > 85
		AND a.s_score <= 100, a.s_score, NULL)) AS 85_100
	, round(COUNT(if(a.s_score > 85
		AND a.s_score <= 100, a.s_score, NULL)) / COUNT(a.s_score) * 100, 2) AS percentage
	, COUNT(if(a.s_score > 70
		AND a.s_score <= 85, a.s_score, NULL)) AS 85_100
	, round(COUNT(if(a.s_score > 70
		AND a.s_score <= 85, a.s_score, NULL)) / COUNT(a.s_score) * 100, 2) AS percentage
	, COUNT(if(a.s_score > 60
		AND a.s_score <= 70, a.s_score, NULL)) AS 85_100
	, round(COUNT(if(a.s_score > 60
		AND a.s_score <= 70, a.s_score, NULL)) / COUNT(a.s_score) * 100, 2) AS percentage
	, COUNT(if(a.s_score > 0
		AND a.s_score <= 60, a.s_score, NULL)) AS 85_100
	, round(COUNT(if(a.s_score > 0
		AND a.s_score <= 60, a.s_score, NULL)) / COUNT(a.s_score) * 100, 2) AS percentage
FROM score a
	JOIN course b ON a.c_id = b.c_id
GROUP BY a.c_id, b.c_name

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

select
a.*,
row_number() over(order by a.avg_score desc) as rm
from(
select
s_id,
round(avg(s_score),2) as avg_score
from score
group by s_id
order by avg_score desc
) a

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

select a.* from (
select
s_id,
c_id,
s_score,
row_number() over(distribute by c_id sort by s_score desc) as rm
from score
) a
where a.rm<=3

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

select
c_id,
count(s_score)
from score
group by c_id

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

SELECT t.s_id, t.s_name
FROM (
	SELECT a.s_id AS s_id, a.s_name AS s_name, COUNT(b.s_score) AS num_course
	FROM student a
		JOIN score b ON a.s_id = b.s_id
	GROUP BY a.s_id, a.s_name
) t
WHERE t.num_course = 2

28、查询男生、女生人数:

select
count(if(s_sex=‘男’,s_sex,null)) as man_number,
count(if(s_sex=‘女’,s_sex,null)) as man_number
from student

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

select * from student where s_name like ‘%风%’

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

select a.s_name,a.num from(
select
s_name,
s_sex,
count(1) over(distribute by s_name,s_sex) as num
from student
) a
where a.num>1

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

select * from student where substr(s_birth,1,4)=‘1990’

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

select a.* from(
select
c_id,
round(avg(s_score),2) as avg_score
from score
group by c_id
) a
order by a.avg_score desc,a.c_id asc

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

select t.* from(
select
a.s_id as s_id,
a.s_name as s_name,
round(avg(b.s_score) over(distribute by b.s_id),2) as avg_score
from student a
join score b on a.s_id=b.s_id
) t
where t.avg_score>=85
group by t.s_id,t.s_name,t.avg_score

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

select
a.s_name,
b.s_score
from student a
join score b on a.s_id=b.s_id
join course c on b.c_id=c.c_id
where c.c_name=‘数学’ and b.s_score<60

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

 

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

select
a.s_name,
c.c_name,
b.s_score
from student a
join score b on a.s_id=b.s_id
join course c on b.c_id=c.c_id
where b.s_score>70

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

select
a.s_name,
c.c_name,
b.s_score
from student a
join score b on a.s_id=b.s_id
join course c on b.c_id=c.c_id
where b.s_score<60
究竟是谁定义的60分为及格呢?

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

select
a.s_id,
a.s_name,
b.s_score
from student a
join score b on a.s_id=b.s_id
where b.c_id=‘01’ and b.s_score>80

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

select
c_id,
count(s_id) as num_course
from score
group by c_id

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

select t.s_id,t.s_name,t.s_birth,t.s_sex,t.s_score from(
select
a.s_id,a.s_name,a.s_birth,a.s_sex,b.s_score,max(b.s_score) over(partition by b.c_id) as max_score
from student a
join score b on a.s_id=b.s_id
join course c on b.c_id=c.c_id
join teacher d on c.t_id=d.t_id and d.t_name=‘张三’
) t
where t.s_score=t.max_score

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

select distinct a.s_id,a.c_id,a.s_score
from score a,score b
where a.c_id!=b.c_id and a.s_score=b.s_score

select distinct a.s_id,a.c_id,a.s_score
from score a,score b
where a.c_id!=b.c_id and a.s_score=b.s_score and a.s_id=b.s_id

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

select a.* from (
select
s_id,
c_id,
s_score,
row_number() over(distribute by c_id sort by s_score desc) as rm
from score
) a
where a.rm<=3

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

select
c_id,
count(s_score) as num_people
from score
group by c_id
having num_people>5
order by num_people desc,c_id asc

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

select
s_id
from score
group by s_id
having count(c_id)>=2

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

SELECT b.*
FROM (
	SELECT s_id, COUNT(c_id) AS num_course
	FROM score
	GROUP BY s_id
) a
	JOIN student b ON a.s_id = b.s_id
	JOIN (
		SELECT COUNT(*) AS total_course
		FROM course
	) c
	ON c.total_course = a.num_course

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

select *,
(case when month(current_date)<month(s_birth)
then year(current_date)-year(s_birth)-1 else year(current_date)-year(s_birth) end
)as age
from student;

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

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

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

select * from student
where weekofyear(s_birth)=weekofyear(current_date)+1

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

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

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

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

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值