SQL Comprehensive Exercise II

A company's HR system has the following tables and data:

**Employee table T_OMS_EMPLOYEE******

| **Field Name****** | **Type****** | **Remarks****** | **Field Description****** |
| ------ ------ | ------------ | ---------- | ------------ |
| EMPNO | NUMERIC(4 ) | NOT NULL | Job ID |
| ENAME | VARCHAR(10) | | Name |
| JOB | VARCHAR(15) | | Position |
| MGR
| Entry Time |
| SAL | NUMERIC(7, 2) | | Salary |
| DEPTNO | NUMERIC(2) | | Department Number |

The table data is as follows:

| **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             |

**Department table T_OMS_DEPT******

| **Field Name****** | **Type****** | **Remarks****** | **Field Description****** |
| ------ ------ | ----------- | ---------- | ------------ |
| DEPTNO | NUMERIC (2) | | Department ID |
| DNAME | VARCHAR(14) | | Department Name |
| LOC | VARCHAR(13) | | Department Location |

The table data is as follows:

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

**Screenplay *****

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'


**Write SQL statements:***

-(1) Query the job number, name, and salary of all employees whose salary is higher than that of JAMES (arranged in descending order of salary)

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

--(2)*Query the names of all employees and the names of their immediate leaders

select a.ename 'name' from employee
question: there is no such column as the name of the direct leader

--(3)*Query the number, name, and department name of all employees whose employment date is earlier than their immediate leader

--When the employee ID of empno is less than the ID of mgr leader, it means that the employee's employment date is earlier than the leader's employment date

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

--(4) Query the number of employees and average salary of each department (the query result must include the department name)

select sum(a.deptno) 'number of employees',avg(a.sal) 'average salary',b.dname 'department name' from employee a,dept b where
a.deptno=b.deptno
group by a.deptno, b.dname

--(5) Query the name, department name and salary of all employees

select a.ename'employee name',b.dname'department name',a.sal'salary' from employee a,department b
where a.deptno=b.deptno

--(6) Query the job number and name of all employees whose position is "CLERK"

select a.empno'employee number', a.ename'employee's name', a.job'CLERK'from employee a
where a.job='CLERK'
--(7)*Query the employees with the lowest salary in each department Job number and name

--Minimum wages for each sector

select deptno,min(sal) 'minimum wage' from employee group by deptno
select a.empno 'employee number',a.ename 'name' from employee a ,(select deptno,min(sal) 'minimum wage' from employee group by deptno) b
where a.deptno=b.deptno and a.sal=b.minimum wage

--(8) Query the name and entry time of the employee whose job number starts with 78

select empno 'name', hiredate 'entry time' from employee where empno like '78%'

--9) Query the job number and name of the employees who joined the company in 1992

select ename 'engine',empno 'last name' from employee where hiredate like '%92%'

--(10) Query the departments without employees

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

Guess you like

Origin blog.csdn.net/m0_64351096/article/details/128015974