Вы находитесь на странице: 1из 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) ); Example: We want to create a table called PROJECT to store information about pro jects. For each project, we want to store the number and the name of the project , the employee number of the projects manager, the budget and the number of perso ns working on the project, and the start date and end date of the project. Furth ermore, we have the following conditions: - a project is identified by its project number, - the name of a project must be unique, - the manager and the budget must be defined. 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: insert into PROJECT(PNO, PNAME, PERSONS, BUDGET, PSTART) values(313, DBS, 4, 150000.42, 10-OCT-94); or insert into PROJECT values(313, DBS, 7411, null, 150000.42, 10-OCT-94, null); Examples: The employee JONES is transfered to the department 20 as a manager an d 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 ma nager 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: In the table EMP only the numbers of the departments are stored, not th eirname. For each salesman, we now want to retrieve the name as well as thenumbe r and the name of the department where he is working: select ENAME, E.DEPTNO, DNAME from EMP E, DEPT D where E.DEPTNO = D.DEPTNO and JOB = SALESMAN;

Example: For each project, retrieve its name, the name of its manager, and the n ame 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; Explanation: The join columns are MGR for the table E1 and EMPNO for the table E 2. The equijoin comparison is E1.MGR = E2.EMPNO. Subqueries Example: List the name and salary of employees of the department 20 who are lead ing a project that started before December 31, 1990: select ENAME, SAL from EMP where EMPNO in (select PMGR from PROJECT where PSTART < 31-DEC-90) and DEPTNO =20; Example:List all employees who are working in a department located in BOSTON: select _ from EMP where DEPTNO in (select DEPTNO from DEPT where LOC = BOSTON); Example: List all those employees who are working in the same department as thei r manager (note that components in [ ] are optional) select _ from EMP E1 where DEPTNO in (select DEPTNO from EMP [E] where [E.]EMPNO = E1.MGR); 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 mo re 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: Assume that we have a table EMP2 that has the same structure and column s as the table EMP: All employee numbers and names from both tables: select EMPNO, ENAME from EMP union select EMPNO, ENAME from EMP2; Employees who are listed in both EMP and EMP2: select _ from EMP intersect select _ from EMP2;

Employees who are only listed in EMP: select _ from EMP minus select _ from EMP2; Example: For each department, we want to retrieve the minimum and maximum salary . select DEPTNO, min(SAL), max(SAL) from EMP group by DEPTNO; Example: Retrieve the minimum and maximum salary of clerks for each department h aving more than three clerks. select DEPTNO, min(SAL), max(SAL) from EMP where JOB = CLERK group by DEPTNO having count(_) > 3; Example: The following view contains the name, job title and the annual salary o f employees working in the department 20: create view DEPT20 as select ENAME, JOB, SAL_12 ANNUAL SALARY from EMP where DEPTNO = 20; In the select statement the column alias ANNUAL SALARY is specified for the expr ession SAL_12 and this alias is taken by the view. An alternative formulation of the above view definition is create view DEPT20 (ENAME, JOB, ANNUAL SALARY) as select ENAME, JOB, SAL _ 12 from EMP where DEPTNO = 20;

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