数据库实验二,嵌套查询、多表连接、子查询

Experiment of Database
Example 2 joins
4. List all departments that do not have any employees.

select deptno
from dept2016150071
where deptno not in (
select deptno from emp2016150071);

5 For each employee whose salary exceeds his manager’s salary, list the employee’s name and salary and the manager’s name and salary.

select e.ename, e.sal, m.ename, m.sal
from emp2016150071 e, emp2016150071 m
where e.mgr = m.empno and e.sal < m.sal

6 List the employees who have BLAKE as their manager.

select e.*, m.ename as Manager
from emp2016150071 e, emp2016150071 m
where e.mgr = m.empno and m.ename = 'BLAKE';

Exercise 6
1 List the name and job of employees who have the same job as Jones.

select ename, job from emp2016150071
where job in (select job from emp2016150071 where ename = 'JONES')
and ename != 'JONES'

2 Find all the employees in Department 10 that have a job that is the same as anyone in department 30.

select * from emp2016150071
where deptno = 10 and job in (
select job from emp2016150071 where deptno = 30)

3 List the name, job, and department of employees who have the same job as Jones or a salary greater than or equal to Ford.

select ename, job, deptno
from emp2016150071
where
job in (select job from emp2016150071 where ename = 'JONES') 
or
sal in (select sal from emp2016150071 where sal > any (select sal from emp2016150071 where ename = 'FORD'))

4 Find all employees in department 10 that have a job that is the same as anyone in the Sales department

select * from emp2016150071
where deptno = 10 and job in 
(select job from emp2016150071
where deptno in 
(select deptno from dept2016150071
where dname = 'SALES'));

5 Find the employees located in Liverpool who have the same job as Allen. Return the results in alphabetical order by employee name.

select * from emp2016150071 e, dept2016150071 d
where e.deptno = d.deptno and loc ='LIVERPOOL' and job in
(select job from emp2016150071 where ename = 'ALLEN')
order by ename;

6 Find all the employees that earn more than the average salary of employees in their department.

select * 
from emp2016150071 e, (select avg(sal) avg, deptno from emp2016150071 group by deptno) temp
where e.deptno = temp.deptno and sal > avg

7 Find all the employees that earn more than JONES, using temporary labels to abbreviate table names.

select * 
from 
  emp2016150071 e1
where 
  (sal+nvl(comm,0)) > any
  (select (sal+nvl(comm,0))from emp2016150071 e2 where ename = 'JONES')

你可能感兴趣的:(数据库系统)