写点什么

Hive 查询语句

作者:mm
  • 2023-01-09
    北京
  • 本文字数:2966 字

    阅读完需:约 10 分钟

数仓经典工具 hive 查询语句练习

创建表

学生表


DROP TABLE IF EXISTS `student`;CREATE TABLE `student` (`s_id` varchar(20),`s_name` varchar(20),`s_birth` varchar(20),`s_sex` varchar(10))row format delimitedfields terminated by ','lines terminated by '\n';
load data local inpath '/root/data/student.csv' into table student;
复制代码

课程表


DROP TABLE IF EXISTS `course`;CREATE TABLE `course` (  `c_id` varchar(20),  `c_name` varchar(20),  `t_id` varchar(20))row format delimitedfields terminated by ','lines terminated by '\n';
load data local inpath '/opt/install/data/course.csv' into table course;
复制代码

教师表


DROP TABLE IF EXISTS `teacher`;CREATE TABLE `teacher`(`t_id` varchar(20),`t_name` varchar(20))row format delimitedfields terminated by ','lines terminated by '\n';
load data local inpath '/opt/install/data/teacher.csv' into table teacher;
复制代码

成绩表


DROP TABLE IF EXISTS `score`;CREATE TABLE `score`(`s_id` varchar(20),`c_id` varchar(20),`s_score` int)row format delimitedfields terminated by ','lines terminated by '\n';
load data local inpath '/opt/install/data/score.csv' into table score;
复制代码


作业内容

  1. 启动 Hadoop(zookeeper + HDFS)

  2. 启动 Hive (后台方式启动 nohup hive --service hiveserver2&)

  3. beeline 连接 hiveserver2 服务

1.启动Hadoop[hadoop@bigdata01 bin]$ sh zkhadoop.sh start
2.启动Hive[hadoop@bigdata03 install]$ nohup hive --service hiveserver2&[1] 22213[hadoop@bigdata03 install]$ nohup: 忽略输入并把输出追加到"nohup.out"
3.beeline连接hiveserver2服务[hadoop@bigdata03 install]$ beeline --color=trueBeeline version 3.1.3 by Apache Hivebeeline> !connect jdbc:hive2://bigdata03:10000Connecting to jdbc:hive2://bigdata03:10000Enter username for jdbc:hive2://bigdata03:10000: hadoopEnter password for jdbc:hive2://bigdata03:10000: Connected to: Apache Hive (version 3.1.3)Driver: Hive JDBC (version 3.1.3)Transaction isolation: TRANSACTION_REPEATABLE_READ0: jdbc:hive2://bigdata03:10000> show tables;+-----------+| tab_name |+-----------+| course || score || student || teacher |+-----------+4 rows selected (3.649 seconds)
复制代码

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


select s.s_id,s.s_name,c.c_id,c.s_score
from student s inner join score c on s.s_id = c.s_id
where s.s_id in
(
select sr.s_id
from (
select s_id,c_id,s_score, rank() over(partition by s_id order by s_score desc) as rn
from score
where c_id ='01' or c_id='02') sr
where sr.rn = 2 and sr.c_id='02'
);
复制代码


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

select s.s_id,s.s_name,c.c_id,c.s_score
from student s inner join score c on s.s_id = c.s_id
where s.s_id in
(
select sr.s_id
from (
select s_id,c_id,s_score, rank() over(partition by s_id order by s_score asc) as rn
from score
where c_id ='01' or c_id='02') sr
where sr.rn = 2 and sr.c_id='02');
复制代码


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

select s.s_id,s.s_name,sv.avg_score from student s inner join (
select s_id,avg(s_score) as avg_score
from score
group by s_id
having avg(s_score) > 60) sv
on s.s_id = sv.s_id;
复制代码


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

select s.s_id,s.s_name,sv.avg_score from student s inner join (
select s_id,avg(s_score) as avg_score
from score
group by s_id
having avg(s_score) < 60) sv
on s.s_id = sv.s_id
union all
select s.s_id,s.s_name,0 as avg_score
from student s left join score sc on s.s_id = sc.s_id
where sc.s_score is null;
复制代码


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

select s.s_id,s.s_name,count(sc.c_id) as course_count,sum(sc.s_score) as score_sum
from student s inner join score sc on s.s_id = sc.s_id
group by s.s_id,s.s_name;
复制代码


查询"李"姓老师的数量

select count(*)
from teacher
where t_name like '李%';
复制代码


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

select s.*
from student s,score sc,course c, teacher t
where s.s_id = sc.s_id and sc.c_id = c.c_id and c.t_id = t.t_id and t.t_name = '张三';
复制代码


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

select * from student
where s_id not in (
select s.s_id
from student s,score sc,course c, teacher t
where s.s_id = sc.s_id and sc.c_id = c.c_id and c.t_id = t.t_id and t.t_name = '张三');
复制代码


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

select *
from student
where s_id in (
select s_id
from score
where c_id='01' or c_id='02'
group by s_id
having count(c_id)>1);
复制代码


查询学过编号为"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 * from student
where s_id in (
select s.s_id
from student s left join (select s_id, count(c_id) as cnt from score group by s_id) sc on s.s_id = sc.s_id
where sc.cnt is NULL or sc.cnt < (select count(*) from course)) ;
复制代码


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

select * from student where s_id in (
select s1.s_id from (select s_id,c_id from score where s_id <> '01') s1 inner join
(select s_id,c_id from score where s_id = '01') s2 on s1.c_id = s2.c_id);
复制代码


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

select * from student
where s_id in (
select b.s_id from (
select s_id, concat_ws(',', collect_set(c_id)) c_ids
from score
where s_id='01'
group by s_id) a inner join (
select s_id, concat_ws(',', collect_set(c_id)) c_ids
from score
group by s_id) b on a.c_ids = b.c_ids );
复制代码


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

select s_name 
from student
where s_id not in (
select sc.s_id
from course c inner join teacher t on c.t_id = t.t_id
inner join score sc on c.c_id = sc.c_id
where t.t_name = '张三')
复制代码


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

select s.s_id,s.s_name, np.avg_score
from student s inner join (
select s_id, avg(s_score) as avg_score
from score
where s_id in (
select s_id
from score
where s_score <60
group by s_id
having count(*) >1)
group by s_id) np on s.s_id = np.s_id;
复制代码


用户头像

mm

关注

还未添加个人签名 2018-08-21 加入

还未添加个人简介

评论

发布
暂无评论
Hive查询语句_mm_InfoQ写作社区