Hive 表 DML 操作 第2关:Select 操作

40 篇文章 5 订阅
17 篇文章 9 订阅

为了完成本关任务,你需要掌握:1. select语法格式;2. 常用的select语法。

select 语法格式

Hive select操作的语法与SQL-92规范几乎没有区别,其格式语法为:

SELECT [ALL | DISTINCT] select_expr,select_expr,… FROM table_reference
[WHERE where_condition] [GROUP BY col_list] [CLUSTER BY col_list | [DISTRIBUTE BY col_list] [SORT BY col_list]] [LIMIT number]

select 与各种属性的组合

  • 简单的select查询操作,如下面的查询操作返回students表中所有的行和列:

    hive> select * from students;

  • WHERE子句的select条件查询操作,返回满足WHERE指定条件的行。如下面的查询操作返回用户信息表users中的年龄大于10岁且国籍为中国的所有用户:

hive> select * from users where age > 10 and state = "China";
  • ALLDISTINCT关键字的查询操作作用于确定是否返回重复的行,默认为ALL,即select查询返回重复的行:

hive> select coll,coll2 from t1;
1 3
1 3
1 4
2 5
hive> select distinct coll,coll2 from t1;
1 3
1 4
2 5
hive> select distinct coll from t1;
1
2
  • HAVING关键字的查询操作用于代替复杂的子查询操作 如查询操作:

    hive> select coll from (select coll,sum(col2) as col2sum from t1 group by coll) t2 where t2.col2sum > 10;

     可以替换为:

hive> select coll from t1 group by col1 having sum(col2) >10;
  • LIMIT关键字的查询操作用于返回指定数目的满足条件的行(常用于返回Top k 问题)。返回满足条件的5条记录,返回结果为从满足条件的记录中随机选取5条。

    select * from t1 limit 5;

  • Top k问题,返回满足条件的列按col1降序排列的前5条记录:

    select * from t1 sort by col1 desc limit 5;

编程要求

test2数据库中student表结构为:

INFOTYPECOMMENT
SnoINTstudent sno
nameSTRINGstudent name
ageINTstudent age
sexSTRINGstudent sex
scoreSTRUCT <Chinese:FLOAT,Math:FLOAT,English:FLOAT>student score

表中的数据为:

  • 切换到test2数据库;

  • 查询student表中所有的行和列;

  • 查询年龄age > 17的女生female

  • 查询语文成绩Chinese > 90的记录;

  • student表中查询前3条记录;

  • 返回按年龄降序的前2条记录。

--Begin
USE test2;
select * from student;
select * from student where age > 17 and sex = "female";
select * from student where score.Chinese > 90;
select * from student limit 3;
select * from student sort by age desc limit 2;

--End

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值