SQL综合练习二

某公司人资系统有如下表及数据:

**员工表T_OMS_EMPLOYEE******

| **字段名称****** | **类型******   | **备注****** | **字段描述****** |
| ------------ | ------------ | ---------- | ------------ |
| EMPNO        | NUMERIC(4)   | NOT NULL   | 工号           |
| ENAME        | VARCHAR(10)  |            | 姓名           |
| JOB          | VARCHAR(15)  |            | 职位           |
| MGR          | NUMERIC(4)   |            | 直属领导工号       |
| HIREDATE     | DATETIME     |            | 入职时间         |
| SAL          | NUMERIC(7,2) |            | 工资           |
| DEPTNO       | NUMERIC(2)   |            | 部门编号         |

该表数据如下:

| **EMPNO****** | **ENAME****** | **JOB****** | **MGR****** | **HIREDATE******        | **SAL****** | **DEPTNO****** |
| ------------- | ------------- | ----------- | ----------- | ----------------------- | ----------- | -------------- |
| 7369          | SMITH         | CLERK       | 7902        | 1990-12-17 00:00:00.000 | 8000        | 20             |
| 7499          | ALLEN         | SALESMAN    | 7698        | 1991-02-20 00:00:00.000 | 16000       | 30             |
| 7521          | WARD          | SALESMAN    | 7698        | 1991-02-22 00:00:00.000 | 12500       | 30             |
| 7566          | JONES         | MANAGER     | 7839        | 1991-04-02 00:00:00.000 | 29750       | 20             |
| 7654          | MARTIN        | SALESMAN    | 7698        | 1991-09-28 00:00:00.000 | 12500       | 30             |
| 7698          | BLAKE         | MANAGER     | 7839        | 1991-05-01 00:00:00.000 | 28500       | 30             |
| 7782          | CLARK         | MANAGER     | 7839        | 1991-06-09 00:00:00.000 | 24500       | 10             |
| 7788          | SCOTT         | ANALYST     | 7566        | 1997-04-19 00:00:00.000 | 30000       | 20             |
| 7839          | KING          | PRESIDENT   |             | 1991-11-17 00:00:00.000 | 50000       | 10             |
| 7844          | TURNER        | SALESMAN    | 7698        | 1991-09-08 00:00:00.000 | 15000       | 30             |
| 7876          | ADAMS         | CLERK       | 7788        | 1997-05-23 00:00:00.000 | 11000       | 20             |
| 7900          | JAMES         | CLERK       | 7698        | 1991-12-03 00:00:00.000 | 9500        | 30             |
| 7902          | FORD          | ANALYST     | 7566        | 1991-12-03 00:00:00.000 | 30000       | 20             |
| 7934          | MILLER        | CLERK       | 7782        | 1992-01-23 00:00:00.000 | 13000       | 10             |

**部门表T_OMS_DEPT******

| **字段名称****** | **类型******  | **备注****** | **字段描述****** |
| ------------ | ----------- | ---------- | ------------ |
| DEPTNO       | NUMERIC (2) |            | 部门ID         |
| DNAME        | VARCHAR(14) |            | 部门名称         |
| LOC          | VARCHAR(13) |            | 部门所在地        |

该表数据如下:

| **DEPTNO****** | **DNAME****** | **LOC****** |
| -------------- | ------------- | ----------- |
| 10             | ACCOUNTING    | NEW YORK    |
| 20             | RESEARCH      | DALLAS      |
| 30             | SALES         | CHICAGO     |
| 40             | OPERATIONS    | BOSTON      |

**脚本 *****

create table department(
DEPTNO NUMERIC(2) primary key,
DNAME varchar(14),
LOC varchar(13)
)

insert into department
select '10','ACCOUNTING','NEW YORK'union
select '20','RESEARCH'  ,'DALLAS' union
select '30','SALES'     ,'CHICAGO'union
select '40','OPERATIONS','BOSTON'

create table employee(
EMPNO varchar(4) primary key not null,
ENAME varchar(10),
JOB varchar(15),
MGR numeric(4),
HIREDATE datetime,
SAL int,
DEPTNO NUMERIC(2) foreign key references department(DEPTNO)
)

insert into employee
select '7369','SMITH'  ,'CLERK'    ,'7902','1990-12-17 00:00:00.000',8000  ,'20' union
select '7499','ALLEN'  ,'SALESMAN' ,'7698','1991-02-20 00:00:00.000',16000  ,'30' union
select '7521','WARD'  ,'SALESMAN' ,'7698','1991-02-22 00:00:00.000',12500  ,'30' union
select '7566','JONES'  ,'MANAGER' ,'7839','1991-04-02 00:00:00.000',29750  ,'20' union
select '7654','MARTIN' ,'SALESMAN' ,'7698','1992-09-28 00:00:00.000',12500 ,'30' union
select '7698','BLAKE'  ,'MANAGER'  ,'7839','1991-05-01 00:00:00.000',28500 ,'30' union
select '7782','CLARK','MANAGER','7839','1991-06-09 00:00:00.000',24500,'10' union
select '7788','SCOTT','ANALYST','7566','1997-04-19 00:00:00.000',30000,'20' union
select '7839','KING'   ,'PRESIDENT',null,'1991-11-17 00:00:00.000',50000 ,'10' union
select '7844','TURNER' ,'SALESMAN' ,'7698','1992-09-08 00:00:00.000',15000 ,'30' union
select '7876','ADAMS'  ,'CLERK'    ,'7788','1997-05-23 00:00:00.000',11000 ,'20' union
select '7900','JAMES'  ,'CLERK'    ,'7698','1991-12-03 00:00:00.000',9500  ,'30' union
select '7902','FORD','ANALYST','7566','1991-12-03 00:00:00.000',30000,'20' union
select '7934','MILLER','CLERK','7782','1992-01-23 00:00:00.000',13000,'10'


**编写SQL语句:***

-(1)查询出工资比JAMES多的所有员工的工号,姓名,工资(按工资降序排列)

select a.empno '工号',a.ename'姓名' ,a.sal '工资' from employee a where a.sal > (
select sal from employee where ename='james')

--(2)*查询出所有员工的姓名以及其直属领导的姓名

select a.ename '姓名' from  employee
疑问:没有直属领导的姓名这个列

--(3)*查询出受雇日期早于其直属领导的所有员工的编号,姓名,部门名称

--当empno 员工工号 小于 mgr 领导工号时,说明员工受雇日期早于领导的受雇日期

select a.empno,a.ename,b.dname from employee a,department b
where a.deptno=b.deptno and a.empno < a.mgr

--(4) 查询出各部门的员工数量和平均工资(查询结果须包括部门名称)

select sum(a.deptno)'员工数量',avg(a.sal) '平均工资',b.dname '部门名称'from  employee a,department b
where a.deptno=b.deptno
group by a.deptno,b.dname

--(5) 查询出所有员工的姓名、部门名称和工资

select a.ename'员工姓名',b.dname '部门名称',a.sal '工资' from  employee a,department b
where a.deptno=b.deptno

--(6) 查询出所有职位为"CLERK"的员工的工号和姓名

select a.empno'员工工号',a.ename '员工姓名',a.job 'CLERK'from  employee a
where a.job='CLERK'
--(7)*查询出各部门工资最低的员工的工号和姓名

--各部门的最低工资

select deptno,min(sal) '最低工资' from  employee group by deptno
select a.empno '工号',a.ename '姓名'from employee a ,(select deptno,min(sal) '最低工资' from  employee group by deptno) b
where a.deptno=b.deptno and a.sal=b.最低工资

--(8) 查询出工号是78开头的员工的姓名和入职时间

select empno '姓名',hiredate '入职时间' from employee where empno like '78%'

--9) 查询出92年入职的员工的工号和姓名

select ename '工号',empno '姓名' from employee where hiredate like '%92%'

--(10) 查询出没有员工的部门

select dname '没有员工的部门' from  employee a
right join department b on a.deptno=b.deptno 
where b.deptno not in (
select a.deptno from  employee a,department b 
where a.deptno=b.deptno)
 

 select * from department 


select * from employee

猜你喜欢

转载自blog.csdn.net/m0_64351096/article/details/128015974
今日推荐