SQL语句练习—员⼯表
以下⾯三个表为例:
create database jepsondb;    /*新建数据库*/
grant all privileges on jepsondb.* to jepsonuser@'%' identified by '123456';
/*赋予所有权限给新建的⽤户jepsonuser,密码是123456*/
flush privileges;    /*刷新权限*/
⼀、部门表:dept部门表(deptno部门编号/dname部门名称/loc地点)
create table dept (
deptno numeric(2),
dname varchar(14),
loc varchar(13)
)
;
insert into dept values (10, 'ACCOUNTING', 'NEW YORK');
insert into dept values (20, 'RESEARCH', 'DALLAS');
insert into dept values (30, 'SALES', 'CHICAGO');
insert into dept values (40, 'OPERATIONS', 'BOSTON');
⼆、⼯资等级表:salgrade⼯资等级表(grade 等级/losal此等级的最低/hisal此等级的最⾼)
create table salgrade (
grade numeric,
losal numeric,
hisal numeric
);
insert into salgrade values (1, 700, 1200);
insert into salgrade values (2, 1201, 1400);
insert into salgrade values (3, 1401, 2000);
insert into salgrade values (4, 2001, 3000);
insert into salgrade values (5, 3001, 9999);
三、员⼯表:emp员⼯表(empno员⼯号/ename员⼯姓名/job⼯作/mgr上级编号/hiredate受雇⽇期/sal薪⾦/comm佣⾦/deptno部门编号)⼯资 = 薪⾦ + 佣⾦
create table emp (
empno numeric(4) not null,
ename varchar(10),
job varchar(9),
mgr numeric(4),
hiredate datetime,
sal numeric(7, 2),
comm numeric(7, 2),
deptno numeric(2)
);
insert into emp values (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, null, 20);
insert into emp values (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30); insert into emp values (7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250, 500, 30); insert into emp values (7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, null, 20); insert into emp values (7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 1400, 30); insert into emp values (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, null, 30); insert into emp values (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, null, 10); insert into emp values (7788, 'SCOTT', 'ANALYST', 7566, '1982-12-09', 3000, null, 20); insert into emp values (7839, 'KING', 'PRESIDENT', null, '1981-11-17', 5000, null, 10);
insert into emp values (7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500, 0, 30); insert into emp values (7876, 'ADAMS', 'CLERK', 7788, '1983-01-12', 1100, null, 20);
insert into emp values (7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950, null, 30);
insert into emp values (7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, null, 20);
insert into emp values (7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, null, 10);
1. 查询出部门编号为30的所有员⼯的编号和姓名
select empno,ename from emp where deptno=30;
2.出部门编号为10中所有经理,和部门编号为20中所有销售员的详细资料。
select * from emp where job='MANAGER' and deptno=10 union all
select * from emp where job='SALESMAN' and deptno=20;
3.查询所有员⼯详细信息,⽤⼯资降序排序,如果⼯资相同使⽤⼊职⽇期升序排序
select * from emp order by (sal+ifnull(comm,0)) desc,hiredate asc;
4.列出薪⾦⼤于1500的各种⼯作及从事此⼯作的员⼯⼈数。
select job,count(*) from emp where sal>1500 group by job;
5.列出在销售部⼯作的员⼯的姓名,假定不知道销售部的部门编号。
select ename from emp where deptno=(select deptno from dept where dname='SALES');
6.查询姓名以S开头的\以S结尾\包含S字符\第⼆个字母为L  __
select ename from emp where ename like 'S%' or ename like '%S' or ename like '%S%' or ename like '_L%';
7.查询每种⼯作的最⾼⼯资、最低⼯资、⼈数
select job, max(sal), min(sal), count(1) from emp group by job;
8.列出薪⾦ ⾼于 公司平均薪⾦的所有员⼯号,员⼯姓名,所在部门名称,上级领导,⼯资,⼯资等级
解:
①⾸先查出平均薪⾦:
select avg(sal) from emp;
②其次查出员⼯表emp(同时命名为e1)的员⼯号、员⼯姓名,并根据员⼯号deptno匹配在部门表dept(同时命名d)查出员⼯所在部门:
select e1.ame,d.dname from emp e1
left join dept d on e1.deptno=d.deptno;
③接着由于上级领导也在员⼯表内,所以需要员⼯表⾃⼰与⾃⼰连接,可以看成是两张相同的表(e1,e2)做连接,先查出⾃⼰(e1表)的上级编号mgr,再与e2匹配,所得就是上级领导命名为leader:
select e1.ame,d.ame as leader
from emp e1
left join dept d on e1.deptno=d.deptno
left join emp e2 =e2.empno;
④然后通过e1的⼯资wage:e1.sal+ifnull(e1m,0)与⼯资表salgrade(命名成s)连接,并⽤判断⼯资等级:
select e1.ame,d.ame as ade,(e1.sal+ifnull(e1m,0)) as wage
from emp e1
left join dept d on e1.deptno=d.deptno
left join emp e2 =e2.empno
left join salgrade s on (e1.sal+ifnull(e1m,0)) between s.losal and s.hisal;
⑤最后再根据①的条件得到最终结果:
select e1.ame,d.ame as ade,(e1.sal+ifnull(e1m,0)) as wage  from emp e1
left join dept d on e1.deptno=d.deptno
sql left join 多表连接
left join emp e2 =e2.empno
left join salgrade s on (e1.sal+ifnull(e1m,0)) between s.losal and s.hisal
where e1.sal > (select avg(sal) from emp);
9.列出薪⾦  ⾼于  在部门30⼯作的  所有/任何⼀个员⼯的薪⾦的员⼯姓名和薪⾦、部门名称。
解法⼀:
①⾸先查出在部门30⼯作的任何⼀个员⼯的最⾼薪⾦:
select max(sal) from emp where deptno=30;
②然后查出满⾜⾼于在部门30⼯作的薪资的所有员⼯姓名和薪⾦:
ame,e.sal
from emp e
where e.sal > (select max(sal) from emp where deptno=30);
③最后通过部门编号与部门表连接查出部门名称:
ame,e.sal,d.dname
from emp e
left join dept d on e.deptno=d.deptno
where e.sal > (select max(sal) from emp where deptno=30);
解法⼆:
ame,e.sal,d.dname from emp e, dept d
where e.deptno=d.deptno and sal > all(select sal from emp where deptno=30);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。