hive---HQL查询

案例6-1 比较运算符应用

1.需求 创建学生emp外部表,向外部表中导入数据,并应用比较运算符做简单查询。

2.数据准备 (1)原始数据 emp表中数据见表6-1。

18999065 王述龙 男 1998-12-10 上海 98 100 2000

18007066 孙宇鹏 男 1999-11-17 沈阳 51 500

18999141 王应龙 男 2000-02-04 沈阳 59 100

18008158 张琼宇 女 1999-07-01 大连 89 200

18999063 宋传涵 女 1999-07-20 上海 86 100 1000

18008009 李亚楠 女 1998-01-24 杭州 97 200 2000

18008026 侯楠楠 男 2000-01-29 北京 79 200

18008027 陈姝元 女 1999-06-24 北京 96 200 1500

18009183 陆春宇 男 1998-01-18 沈阳 87 300 1000

18009173 孙云琳 女 1997-07-15 上海 56 300

18008014 尤骞梓 女 1999-04-25 杭州 86 200 1000

18998002 张爱林 男 1999-05-16 北京 92 400 1500

18009019 曹雪东 男 2000-11-20 北京 78 300

18998153 贾芸梅 女 2000-06-12 大连 88 400 1000

18007051 温勇元 男 1999-08-08 上海 65 500

18998039 张微微 女 1998-01-27 北京 90 400 1500

18007063 李君年 男 1998-03-21 上海 78 500

18007095 卢昱泽 女 1998-08-01 上海 57 500

18007096 赵旭辉 男 1999-02-18 北京 75 500

18009087 张矗年 男 1997-07-26 重庆 86 300 1000

(2)创建本地数据文件emp.txt vim emp.txt 并将表6-1中数据导入其中,然后保存并退出。

3.Hive实例操作

(1)创建外部表emp

create external table if not exists emp(

empno int,

ename string,

gender string,

bday string,

area string,

score double,

deptno int,

scholarship double)

row format delimited fields terminated by '\t';

(1.1)创建dept表

CREATE TABLE dept(

deptno INT,

deptname STRING,

buildingno STRING

)

ROW FORMAT DELIMITED

FIELDS TERMINATED BY '\t'

STORED AS TEXTFILE;

(2)向外部表emp中导入数据

load data local inpath '/opt/datas/emp.txt' into table emp;
load data local inpath '/opt/datas/dept.txt' into table dept;

(3)设置汉字编码,否则汉字出现乱码

alter table emp set serdeproperties('serialization.encoding'='utf-8');

(4)查询成绩为98分的学生的所有信息

select empno,ename,gender,bday,area,score,deptno from emp where score =98;

(5)查询成绩为90分到100分的学生信息

select empno,ename,gender,area,score,deptno from emp where score between 90 and 100;

(6)查询奖学金scholarship不为空的所有学生信息

select empno,ename,gender,area,score,scholarship from emp where scholarship is not null;

(7)查询成绩为86分或96分的学生信息

hive(hivedwh)>select ename,gender,area,score,deptno from emp where score IN (86, 96);

(8)查询成绩大于90分的所有学生

select empno,ename,gender,bday,area,score,deptno from emp where score >90;

1.查询成绩以9开头的学生信息

selectempno,ename,gender,bday,area,score from emp where score LIKE '9%';

2.查询成绩中第二个数值为8的学生信息

select ename,area,score,deptno,scholarship from emp where score LIKE '_8%';

3.查询出生日期中含有6的学生信息,正则表达式仅限于字符串类型的字段

select empno,ename,bday,area,score,deptno from emp where bday RLIKE '6';

案例6-3 逻辑运算符应用

1.查询成绩大于80分,系别是300的学生信息

select empno,ename,bday,area,score,deptno

from emp

where score>80 and deptno=300;

2.查询成绩大于95分,或者系别是100的学生信息

select ename,gender,bday,area,score,deptno from emp where score>95 or deptno=100;

3.查询除系别200和系别500外的学生信息

hive(hivedwh)>select empno,ename,bday,area,score,deptno from emp where deptno not IN(200, 500);

案例6-4 Group By语句应用

1.查询emp表每个部门的平均成绩

select deptno, avg(score) avg_score from emp group by deptno;

2.查询emp表每个部门中男女性别的最好成绩

select deptno, gender, max(score) max_score from emp group by deptno, gender;

Hive实例操作

1.查询emp表中平均成绩大于80分的部门:

select deptno, avg(score) avg_score from emp group by deptno having avg_score > 80;

6.4.1 等值连接

SELECT e.empno, e.ename, d.deptno, d.deptname

FROM emp

e JOIN dept d ON e.deptno = d.deptno;

使用表的别名可以简化查询,还可以提高执行效率。 例如,合并dept表和emp表:

select e.empno, e.ename, d.deptno from emp e join dept d on e.deptno = d.deptno;

6.4.4 左外连接Left Outer Join

SELECT e.empno, e.ename, e.deptno

FROM emp

e LEFT SEMI JOIN dept d ON e.deptno = d.deptno;

相关推荐
夜雪一千4 小时前
MySQL查询条件的顺序是否影响查询效率
数据库·mysql
X-⃢_⃢-X4 小时前
十、Redis之布隆过滤器
数据库·redis·缓存
Nturmoils4 小时前
订单号查出了三笔,我以为是数据脏了,其实是自己写错了
数据库
ZKNOW甄知科技4 小时前
燕千云深度集成飞书:以AI之力,开启无感IT运维体验
大数据·运维·网络·数据库·人工智能·低代码·集成学习
TDengine (老段)4 小时前
TDengine 免费版说明
java·大数据·数据库·物联网·时序数据库·tdengine
汉知宝科技4 小时前
历史案件数据迁移:知识产权管理系统落地的第一道门槛
大数据·数据库
这就是佬们吗4 小时前
Python入门⑤-异常处理、文件操作与实战项目
开发语言·数据库·python·算法·pycharm
吴声子夜歌4 小时前
MongoDB 4.x——SpringBoot框架整合
数据库·spring boot·mongodb
凤山老林5 小时前
MySQL“读已提交“并非万能药:深度解析RC隔离级别的盲区与适用边界
数据库·mysql
Database_Cool_5 小时前
阿里云 Tair(企业级内存数据库)vs 腾讯云 Redis 云缓存深度对比:性能/数据结构/成本全维度 Benchmark
数据库·阿里云·缓存