HQL作业
Posted 安小猿
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HQL作业相关的知识,希望对你有一定的参考价值。
员工信息表emp:
字段:员工id,员工名字,工作岗位,部门经理,受雇日期,薪水,奖金,部门编号
英文名:EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,BONUS,DEPTNO
create table emp(
EMPNO int
,ENAME string
,JOB string
,MGR int
,HIREDATE string
,SAL int
,BONUS int
,DEPTNO int
)
row format delimited
fields terminated by ',';
7369,SMITH,CLERK,7902,1980-12-17,800,null,20
7499,ALLEN,SALESMAN,7698,1981-02-20,1600,300,30
7521,WARD,SALESMAN,7698,1981-02-22,1250,500,30
7566,JONES,MANAGER,7839,1981-04-02,2975,null,20,
7654,MARTIN,SALESMAN,7698,1981-09-28,1250,1400,30
7698,BLAKE,MANAGER,7839,1981-05-01,2850,null,30
7782,CLARK,MANAGER,7839,1981-06-09,2450,null,10
7788,SCOTT,ANALYST,7566,1987-04-19,3000,null,20
7839,KING,PRESIDENT,null,1981-11-17,5000,null,10
7844,TURNER,SALESMAN,7698,1981-09-08,1500,0,30
7876,ADAMS,CLERK,7788,1987-05-23,1100,null,20
7900,JAMES,CLERK,7698,1981-12-03,950,null,30
7902,FORD,ANALYST,7566,1981-12-03,3000,null,20
7934,MILLER,CLERK,7782,1982-01-23,1300,null,10
1600 1250 2850 1500 950
部门信息表dept:
字段:部门编号,部门名称,部门地点
英文名:DEPTNO,DEPTNAME,DEPTADDR
create table dept(
DEPTNO int
,DEPTNAME string
,DEPTADDR string
)
row format delimited
fields terminated by ',';
10,ACCOUNTING,NEW YORK
10,ACCOUNTING,shanghai
20,RESEARCH,DALLAS
30,SALES,CHICAGO
40,OPERATIONS,BOSTON
1. 列出至少有一个员工的所有部门。
select deptno,count(*) sum from emp group by deptno having sum>1 ;
2. 列出薪金比“SMITH”多的所有员工。
##在mysql中可以使用,在HIVE中不支持不等式子查询
##select * from emp where sal>(select sal from emp where ename='SMITH');
select *
from (select 1 as tmp_id,empno,ename,sal from emp where ename!='SMITH') t1
left join (select 1 as tmp_id,sal from emp where ename='SMITH') t2
on t1.emp_id = t2.emp_id
where t1.sal > t2.sal;
3. 列出所有员工的姓名及其直接上级的姓名。
select emp1.ename,emp2.ename from emp emp1 left join (select empno,ename from emp ) emp2 on emp1.mgr=emp2.empno;
4. 列出受雇日期早于其直接上级的所有员工。
select emp1.*
from emp emp1
left join emp emp2
on emp1.mgr=emp2.empno
where emp1.hiredate< emp2.hiredate;
5. 列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门。
select dept.deptname,emp.* from dept left join emp on dept.deptno=emp.deptno;
6. 列出所有“CLERK”(办事员)的姓名及其部门名称。
select emp.ename,dept.deptname from emp left join dept on emp.deptno=dept.deptno where emp.job='CLERK';
7. 列出最低薪金大于1500的各种工作。
select distinct job ,min(sal) min_sal from emp where min_sal>1500;
8. 列出在部门“SALES”(销售部)工作的员工的姓名,假定不知道销售部的部门编号
select ename from emp emp1 left join dept on emp.deptno = dept.deptno where deptname='SALES';
9. 列出薪金高于公司平均薪金的所有员工。
##select * from emp where sal>(select avg(sal) from emp);
##SQL中可用,HQL中不可用
select emp1.*
from (select 1 as tmp_id,* from emp) emp1
left join (select 1 as tmp_id,avg(sal) avg_sal from emp ) emp2
on emp1.tmp_id = emp2.tmp_id
where emp1.sal>emp2.avg_sal;
10.列出与“SCOTT”从事相同工作的所有员工。
select * from emp emp1 where ename != 'SCOTT' and job in (select job from emp emp2 where ename='SCOTT');
11.列出薪金等于部门30中员工的薪金的所有员工的姓名和薪金。
select ename,sal from emp emp1 where sal in (select sal from emp emp2 where deptno=30);
12.列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。
##select ename,sal from emp where sal >(select min(sal) from emp where deptno=30);
##SQL中可用,HQL中不可用
select emp1.ename,emp1.sal
from (select 1 tmp_id , ename,sal from emp ) emp1
left join (select 1 tmp_id,sal from emp where deptno=30) emp2
on emp1.tmp_id=emp2.tmp_id
where emp1.sal >emp2.sal;
13.列出在每个部门工作的员工数量、平均工资和平均服务期限。
select count(*) sum,avg(sal+(case when bonus is null then 0 else bonus end)) avg_money,avg(datediff(current_date,hiredate)) avg_time from emp group by deptno;
14.列出所有员工的姓名、部门名称和工资。
select ename,deptname,sal+(case when bonus is null then 0 else bonus end) from emp left join dept on emp.deptno=dept.deptno ;
15.列出所有部门的详细信息和部门人数。
select dept.*,sum from dept left join (select deptno,count(*) sum from emp group by deptno) emp1 on dept.deptno=emp1.deptno;
16.列出各种工作的最低工资。
select min(sal+(case when bonus is null then 0 else bonus end)) from emp group by job;
17.列出各个部门的MANAGER(经理)的最低薪金。
select sal from emp emp1 where sal=(select min(sal) from emp emp2 where emp1.deptno=emp2.deptno);
18.列出所有员工的年工资,按年薪从低到高排序。
select 12*(sal+(case when bonus is null then 0 else bonus end)) sum_sal from emp order by sum_sal;
19. 列出每个部门薪水前两名最高的人员名称以及薪水。
select ename,sal from emp emp1 where 2>(select count(*) from emp emp2 where emp1.deptno=emp2.deptno and emp1.sal<emp2.sal);
select emp1.ename,emp1.sal from (select ename,sal,row_number() over (partition by deptno order by sal desc ) as rn from emp ) emp1 where rn<=2;
20. 列出每个员工从受雇开始到2018-12-12 为止共受雇了多少天。
select *,datediff('2018-12-12',hiredate) from emp;
以上是关于HQL作业的主要内容,如果未能解决你的问题,请参考以下文章
HTML5期末大作业:餐饮美食网站设计——咖啡(10页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 咖啡网页设计 美食餐饮网页设计...(代码片段