You are on page 1of 5

DROP TABLE EMP; DROP TABLE DEPT; DROP TABLE BONUS; DROP TABLE SALGRADE; DROP TABLE DUMMY;

CREATE TABLE EMP (EMPNO NUMBER(4) NOT NULL, ENAME VARCHAR2(10), JOB VARCHAR2(9), MGR NUMBER(4), HIREDATE DATE, SAL NUMBER(7, 2), COMM NUMBER(7, 2), DEPTNO NUMBER(2)); INSERT INTO EMP VALUES (7369, 'SMITH', 'CLERK', 7902, TO_DATE('17-DEC-1980', 'DD-MON-YYYY'), 800, NULL, 20); INSERT INTO EMP VALUES (7499, 'ALLEN', 'SALESMAN', 7698, TO_DATE('20-FEB-1981', 'DD-MON-YYYY'), 1600, 300, 30); INSERT INTO EMP VALUES (7521, 'WARD', 'SALESMAN', 7698, TO_DATE('22-FEB-1981', 'DD-MON-YYYY'), 1250, 500, 30); INSERT INTO EMP VALUES (7566, 'JONES', 'MANAGER', 7839, TO_DATE('2-APR-1981', 'DD-MON-YYYY'), 2975, NULL, 20); INSERT INTO EMP VALUES (7654, 'MARTIN', 'SALESMAN', 7698, TO_DATE('28-SEP-1981', 'DD-MON-YYYY'), 1250, 1400, 30); INSERT INTO EMP VALUES (7698, 'BLAKE', 'MANAGER', 7839, TO_DATE('1-MAY-1981', 'DD-MON-YYYY'), 2850, NULL, 30); INSERT INTO EMP VALUES (7782, 'CLARK', 'MANAGER', 7839, TO_DATE('9-JUN-1981', 'DD-MON-YYYY'), 2450, NULL, 10); INSERT INTO EMP VALUES (7788, 'SCOTT', 'ANALYST', 7566, TO_DATE('09-DEC-1982', 'DD-MON-YYYY'), 3000, NULL, 20); INSERT INTO EMP VALUES (7839, 'KING', 'PRESIDENT', NULL, TO_DATE('17-NOV-1981', 'DD-MON-YYYY'), 5000, NULL, 10); INSERT INTO EMP VALUES (7844, 'TURNER', 'SALESMAN', 7698,

'DD-MON-YYYY'). LOSAL NUMBER. HISAL NUMBER). 20). 'OPERATIONS'. 'MILLER'. 1401. TO_DATE('12-JAN-1983'. 'DD-MON-YYYY'). 7698. 1200). 3000. SAL NUMBER. TO_DATE('3-DEC-1981'. NULL. NULL. 9999). INSERT INTO DEPT VALUES (20. DNAME VARCHAR2(14). 20). TO_DATE('3-DEC-1981'. 'DD-MON-YYYY'). 3001. COMM NUMBER). INSERT INTO EMP VALUES (7934. 'NEW YORK'). JOB VARCHAR2(9). CREATE TABLE SALGRADE (GRADE NUMBER. INSERT INTO SALGRADE VALUES (5. 7566. 'DALLAS'). 'SALES'. 2001. 30). 3000). 'RESEARCH'. 1201. 10). 'JAMES'. NULL. 'CLERK'. 2000). 7782. 'ANALYST'. INSERT INTO DEPT VALUES (30. 'CHICAGO'). 'FORD'. INSERT INTO EMP VALUES (7902. INSERT INTO SALGRADE VALUES (1. INSERT INTO EMP VALUES (7900. 'DD-MON-YYYY'). CREATE TABLE DEPT (DEPTNO NUMBER(2). 1500. INSERT INTO SALGRADE VALUES (4. INSERT INTO SALGRADE VALUES (2. CREATE TABLE BONUS (ENAME VARCHAR2(10). INSERT INTO DEPT VALUES (10. 30). 950. TO_DATE('23-JAN-1982'. 7788.TO_DATE('8-SEP-1981'. 'ACCOUNTING'. 'BOSTON'). INSERT INTO EMP VALUES (7876. LOC VARCHAR2(13) ). 1100. 1300. 'ADAMS'. 'CLERK'. 'DD-MON-YYYY'). 0. 'CLERK'. 1400). INSERT INTO DEPT VALUES (40. 700. . INSERT INTO SALGRADE VALUES (3. NULL.

e.deptno and d. 2.losal and s.dept where emp.job.dept d where e.avg(sal) from emp group by job. select job avg(sal) from emp where job!=’MANAGER’ group by job. select deptno.d. 2. so in order to get that record we are using outer join with employee deptno.Day 22-10-2011 – to be executed queries Group by clause: Using this we can get values by grouping similar values: 1.grade from emp e.hisal Other joins:-OUTER JOIN Here we use ‘(+)’ operator for outer join using this join we can get the rows which doesn’t satisfies the condition while accessing data from two tables 1. SET Operations: . max(sal) from emp having max(sal) >3000 group by job.dept where emp. select ename.d. dname from emp.s. select job.sal between s.ename.deptno(+)=d.deptno category 2: non-equi join in this we join two tables which doesn’t have same column name 1.ename. Having clause: Using this we can give the conditions to group values 1.deptno.deptno order by dept. ename.deptno=dept.select e.40) explanation: in EMP table we don’t have record with deptno 40 but we have a record in DEPT table with deptno 40. select dept.dname from emp e. select e.deptno. select job.avg(sal) from emp group by deptno having count(*) > 3 2. job.’ Operator. JOINS: Here we are going to learn how to join two distinct tables: Category 1 : Equi joins In this we compare two columns which are present in both tables 1.deptno in (30.dname from emp.deptno explanation: in order to distinguish two deptno’s here we used table name before the column name with ‘.deptno=dept.salgrade s where e.sal.

Select * from d10view .create view d10view as select empno.ename.sal from emp where deptno=10 Explanation : Here we are creating a view with name d10-view it consists only the employee details who are working in deptno 10 By this way we can restrict the users from accessing entire table Now user can get the data from this view by writing the command 1.UNION UNION ALL INTERSECT MINUS 1.select job from emp where deptno=10 UNION select job from emp where deptno=30 Apply all other set operations in the above said way and check the results VIEW’s A view is a virtual table which can prepare by combining several columns from different tables 1.