set hive.cli.print.header=true;
set hive.resultset.use.unique.column.names=false;
SELECT [ALL | DISTINCT] select_expr, select_expr, ...
FROM table_reference
[WHERE where_condition]
[GROUP BY col_list]
[ORDER BY col_list]
[CLUSTER BY col_list
| [DISTRIBUTE BY col_list] [SORT BY col_list]
]
[LIMIT number]
在 /root/hivedata 下面创建两个文件
10 ACCOUNTING 1700
20 RESEARCH 1800
30 SALES 1900
40 OPERATIONS 1700
7369 SMITH CLERK 7902 1980-12-17 800.00 20
7499 ALLEN SALESMAN 7698 1981-2-20 1600.00 300.00 30
7521 WARD SALESMAN 7698 1981-2-22 1250.00 500.00 30
7566 JONES MANAGER 7839 1981-4-2 2975.00 20
7654 MARTIN SALESMAN 7698 1981-9-28 1250.00 1400.00 30
7698 BLAKE MANAGER 7839 1981-5-1 2850.00 30
7782 CLARK MANAGER 7839 1981-6-9 2450.00 10
7788 SCOTT ANALYST 7566 1987-4-19 3000.00 20
7839 KING PRESIDENT 1981-11-17 5000.00 10
7844 TURNER SALESMAN 7698 1981-9-8 1500.00 0.00 30
7876 ADAMS CLERK 7788 1987-5-23 1100.00 20
7900 JAMES CLERK 7698 1981-12-3 950.00 30
7902 FORD ANALYST 7566 1981-12-3 3000.00 20
7934 MILLER CLERK 7782 1982-1-23 1300.00 10
create table if not exists dept(depton int,dname string,loc int)
row format delimited fields terminated by '\t';
create table if not exists emp(empno int,ename string,job string,mgr int,hiredate string,sal double,comm double,deptno int)
row format delimited fields terminated by '\t';
load data local inpath '/root/hivedata/dept.txt' into table dept;
load data local inpath '/root/hivedata/emp.txt' into table emp;
select * from emp;
select empno,ename from emp;
注意:
案例实操
select ename as name,deptno dn from emp;
运算符 | 描述 |
---|---|
A+B | A 和 B 相加 |
A-B | A 减去 B |
A/B | A 除以 B |
A*B | A 和 B 相乘 |
– | – |
select sal +1 from emp;
select count(*) cnt from emp;
select max(sal) max_sal from emp;
select min(sal) min_sal from emp;
select sum(sal) sum_sal from emp;
select avg(sal) avg_sal from emp;
select * from emp limit 5;
案例实操
select ename,sal from emp where sal > 1000;
案例实操
select ename,sal from emp where sal=5000;
select ename,sal from emp where between 500 and 1000;
select ename,comm from emp where comm is null;
select ename,sal from emp where sal in (1500,5000);
select * from emp where sal>1000 and deptno=30;
select * from emp where sal>1000 or deptno=30;
select * from emp where deptno not in(20,30);
GROUP BY 语句通常会和聚合函数一起使用,按照一个或者多个列队结果进行分组,然后对每个组执行聚合操作。
案例实操
select deptno,avg(sal) avg_sal from emp group by deptno;
select deptno,job,max(sal) max_sal from emp group by deptno,job;
(1)where 后面不能写分组函数,而 having 后面可以使用分组函数。
(2)having 只用于 group by 分组统计语句。
select deptno,avg(sal) avg_sal from emp group by deptno having avg_sal>2000;
select e.empno,e.ename,d.dname from emp e join dept d on e.deptno = d.depton;
(1)使用别名可以简化查询。
(2)使用表名前缀可以提高执行效率
案例实操
select * from emp order by sal;
select * from emp order by sal desc;
select ename,sal*2 twosal from emp order by twosal;
select ename,deptno,sal from emp order by deptno,sal;
set mapreduce.job.reduces=3;
set mapreduce.job.reduces;
select * from emp sort by deptno desc;
insert overwrite local directory '/root/hivedata'
select * from emp sort by deptno desc;