/--部门表
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);
select * from dept;
select * from emp;
select * from salgrade;
-- 1. 查询出部门编号为30的所有员工的编号和姓名
select empno,
ename
from emp
where deptno=30;
-- 2.找出部门编号为10中所有经理,和部门编号为20中所有销售员的详细资料。
select *
from emp
where (deptno = 10 and job = "MANAGER")
or (deptno = 20 and job = "SALESMAN");
-- 3.查询所有员工详细信息,用工资降序排序,如果工资相同使用入职日期升序排序
select *
from emp
order by sal desc ,hiredate asc;
-- 4.列出薪金大于1500的各种工作及从事此工作的员工人数。
select job,count(*)
from emp
where sal >1500
group by job
having count(*);
-- 5.列出在销售部工作的员工的姓名,假定不知道销售部的部门编号。
select ename
from emp,dept
where dept.dname = "SALES" and (emp.deptno = dept.deptno );
-- 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(*)
from emp
group by job;
-- 8.列出薪金 高于 公司平均薪金的所有员工号,员工姓名,所在部门名称,上级领导,工资,工资等级
select
emp.empno,
emp.ename,
dept.dname,
emp.mgr,
emp.sal,
-- salgrade.grade ,
salgrade.grade
from emp left join dept on emp.deptno = dept.deptno
left join salgrade on sal between salgrade.losal and salgrade.hisal
where emp.sal > (select avg(emp.sal) from emp);
-- 9.列出薪金 高于 在部门30工作的 所有/任何一个员工的薪金的员工姓名和薪金、部门名称。
select
e.ename,
e.sal,
d.dname
from
emp e left join dept d on e.deptno = d.deptno
where sal >all(
select sal
from emp e2
where e2.deptno = 30
);