建表语句:
员工信息表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
部门信息表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 count(),deptno from emp group by deptno having count()>=1;
2. 列出薪金比“SMITH”多的所有员工。
MySQL:
select * from emp where sal>(
select sal from emp where ename=“SMITH”
);
Hive:
select a.ename,a.sal from
(select ename,sal,1 as cid from emp) as a
left join
(select sal ,1 as cid from emp where ename=‘SMITH’) as b
on a.cid=b.cid where a.sal>b.sal;
3. 列出所有员工的姓名及其直接上级的姓名。
MySQL:
select worker.ename,boss.ename
from emp as worker
left join emp as boss
on worker.mgr=boss.empno
Hive:
select a.ename,b.ename from
(select ename,mgr from emp) as a
left join
(select ename,empno from emp) as b
on a.mgr=b.empno
4. 列出受雇日期早于其直接上级的所有员工。
select worker.ename,boss.ename
from
(select empno,ename,hiredate from emp) as boss
right join
(select mgr,ename,hiredate from emp) as worker
on boss.empno=worker.mgr
where boss.hiredate>worker.hiredate
5. 列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门。
select * from emp as t1
left join dept as t2
on t1.deptno=t2.deptno
6. 列出所有“CLERK”(办事员)的姓名及其部门名称。
select t1.ename,t2.deptname
from
(select ename,deptno from emp where job=‘CLERK’) as t1
left join
dept as t2
on t1.deptno=t2.deptno
7. 列出最低薪金大于1500的各种工作。
select job
from
(select job,min(sal) as min_sal from emp group by job) as t
where min_sal > 1500
8. 列出在部门“SALES”(销售部)工作的员工的姓名,假定不知道销售部的部门编号
select t1.ename
from emp t1
join
dept t2
on t1.deptno=t2.deptno
where t2.deptname=‘SALES’
select t1.name
from emp t1
join
(select deptno from dept where deptname=‘SALES’) t2
on t1.deptno=t2.deptno
9. 列出薪金高于公司平均薪金的所有员工。
select t1.ename,t1.sal ,t2.avg_sal
from
(select ename,sal,1 as cid from emp) as t1
join
(select avg(sal) as avg_sal,1 as cid from emp) as t2
on t1.cid=t2.cid
where t1.sal>t2.avg_sal
10.列出与“SCOTT”从事相同工作的所有员工。
select t1.ename
from
(select ename,job,1 as cid from emp) as t1
join
(select job,1 as cid from emp where ename=‘SCOTT’) as t2
on t1.cid=t2.cid
where t1.job=t2.job and t1.ename!=‘SCOTT’
11.列出薪金 等于部门30中 员工的薪金 的所有员工的姓名和薪金。
select t1.ename,t1.sal
from emp as t1
join
(select distinct SAL from emp where DEPTNO = 30) t2
on t1.sal=t2.sal
where t1.deptno!=30
12.列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。
select t1.ename,t1.sal
from (select ename,sal,1 as cid from emp where deptno!=30) as t1
left join
(select max(sal) as max_sal,1 as cid from emp where deptno=30) as t2
on t1.cid=t2.cid
where t1.sal>t2.max_sal
13.列出在每个部门工作的员工数量、平均工资和平均服务期限。
select t1.deptno,t1.people,t2.avg_sal,t3.avg_time
from
(select count(*) as people,deptno from emp group by deptno) as t1
left join
(select round(avg(sal),2) as avg_sal,deptno from emp group by deptno) as t2
on t1.deptno=t2.deptno
left join
(select round(avg(datediff(current_date,hiredate)),2) as avg_time,deptno from emp group by deptno) as t3
on t2.deptno=t3.deptno
14.列出所有员工的姓名、部门名称和工资。
select t1.ename,t2.deptname,t1.sal
from emp as t1
left join
dept as t2
on t1.deptno=t2.deptno
15.列出所有部门的详细信息和部门人数。
select *
from
(select * from dept) as t1
left join
(select deptno,count(*) from emp group by deptno) as t2
on t1.deptno=t2.deptno
16.列出各种工作的最低工资。
select job,min(sal) from emp group by job
17.列出各个部门的MANAGER(经理)的最低薪金。
select deptno,ename,min(sal) from emp where job=‘MANAGER’ group by deptno,ename
18.列出所有员工的年工资,按年薪从低到高排序。
select ename,(sal+if(bonus is null,0,bonus))*12 as year_sal from emp
order by year_sal
-
列出每个部门薪水最高的前两名人员名称以及薪水。
select ename,sal
from
(select ename,sal,deptno,row_number() over(partition by deptno order by sal desc) as rk
from emp) as t
where rk<=2 -
列出每个员工从受雇开始到2018-12-12 为止共受雇了多少天。
select ename,datediff(‘2018-12-12’,hiredate) from emp
感谢阅读,我是啊帅和和,一位大数据专业大四学生,祝你快乐。