Вы находитесь на странице: 1из 2

Example: Retrieve all employees who are working in department 10 and who earn at least as much as

any (i.e., at least one) employee working in department 30: select ∗ from EMP where SAL >= any (select
SAL from EMP where DEPTNO = 30) and DEPTNO = 10;

Example: List all employees who are not working in department 30 and who earn more than all
employees working in department 30: select ∗ from EMP where SAL > all (select SAL from EMP where
DEPTNO = 30) and DEPTNO <> 30;

Example: List all departments that have no employees: select ∗ from DEPT where not exists (select ∗
from EMP where DEPTNO = DEPT.DEPTNO);

Example: Retrieve the minimum and maximum salary of clerks for each department having more than
three clerks. select DEPTNO, min(SAL), max(SAL) from EMP where JOB = ’CLERK’ group by DEPTNO
having count(∗) > 3;

Example: List the job title and the salary of those employees whose manager has the number 7698 or
7566 and who earn more than 1500: select JOB, SAL from EMP where (MGR = 7698 or MGR = 7566)
and SAL > 1500;

Example: The create table statement for our EMP table has the form create table EMP ( EMPNO
number(4) not null, ENAME varchar2(30) not null, JOB varchar2(10), MGR number(4), HIREDATE date,
SAL number(7,2), DEPTNO number(2) ); Remark: Except for the columns EMPNO and ENAME null values
are allowed.

Example: All salesmen working in the department 20 get the same salary as the manager who has the
lowest salary among all managers. update EMP set SAL = (select min(SAL) from EMP where JOB =
’MANAGER’) where JOB = ’SALESMAN’ and DEPTNO = 20;

Example: For each project, retrieve its name, the name of its manager, and the name of the department
where the manager is working: select ENAME, DNAME, PNAME from EMP E, DEPT D, PROJECT P where
E.EMPNO = P.MGR and D.DEPTNO = E.DEPTNO;

Example: List the names of all employees together with the name of their manager: select E1.ENAME,
E2.ENAME from EMP E1, EMP E2 where E1.MGR = E2.EMPNO;
Example: Retrieve all employees who are working in department 10 and who earn at least as much as
any (i.e., at least one) employee working in department 30: select ∗ from EMP where SAL >= any (select
SAL from EMP where DEPTNO = 30) and DEPTNO = 10;

Example: List all departments that have no employees: select ∗ from DEPT where not exists (select ∗
from EMP where DEPTNO = DEPT.DEPTNO);

Example: List all employees who are not working in department 30 and who earn more than all
employees working in department 30: select ∗ from EMP where SAL > all (select SAL from EMP where
DEPTNO = 30) and DEPTNO <> 30;

Table definition: create table PROJECT ( PNO number(3) constraint prj pk primary key, PNAME
varchar2(60) unique, PMGR number(4) not null, PERSONS number(5), BUDGET number(8,2) not null,
PSTART date, PEND date);

Examples: • The employee JONES is transfered to the department 20 as a manager and his salary is
increased by 1000: update EMP set JOB = ’MANAGER’, DEPTNO = 20, SAL = SAL +1000 where ENAME =
’JONES’; • All employees working in the departments 10 and 30 get a 15% salary increase. update EMP
set SAL = SAL ∗ 1.15 where DEPTNO in (10,30);

Example: All salesmen working in the department 20 get the same salary as the manager who has the
lowest salary among all managers. update EMP set SAL = (select min(SAL) from EMP where JOB =
’MANAGER’) where JOB = ’SALESMAN’ and DEPTNO = 20;

Example: List the job title and the salary of those employees whose manager has the number 7698 or
7566 and who earn more than 1500: select JOB, SAL from EMP where (MGR = 7698 or MGR = 7566)
and SAL > 1500;

Example: List all employees who are working in a department located in BOSTON: 13 select ∗ from EMP
where DEPTNO in (select DEPTNO from DEPT where LOC = ’BOSTON’);

The following view contains the name, job title and the annual salary of employees working in the
department 20: create view DEPT20 as select ENAME, JOB, SAL∗12 ANNUAL SALARY from EMP where
DEPTNO = 20;

Вам также может понравиться