create table dept39(dno number(2) constraint dept_pk_39 primary key,dname varchar2(15),dloc varchar2(10));

Table created.

create table employee39(empno number(5) constraint employee_pk_39 primary key,empname varchar2(15),designation varchar2(10),date_of_join date constraint emp_doj_NN_39 NOT NULL,salary number(9,2),empdno number(2) constraint emp_dno_fk_39 references dept39(dno));
Table created.

create table course39(coursecode number(2), coursename varchar2(15), constraint codeno_pk_39 primary key(coursecode));
Table created.

create table student39(rollno number(5), name varchar2(15), coursecode number(2), mark1 number(3), mark2 number(3), constraint rn_pk_39 primary key(rollno), constraint cc_fk_39 foreign key(coursecode) references course39(coursecode), constraint ma1check_39 check(mark1>=0 and mark1<=100), constraint ma2check_39 check(mark2>=0 and mark2<=100));
Table created.

desc dept39
Name DNO DNAME DLOC Null? NOT NULL Type NUMBER(2) VARCHAR2(15) VARCHAR2(10) Type NUMBER(5) VARCHAR2(15) VARCHAR2(10) DATE NUMBER(9,2) NUMBER(2) Type NUMBER(2) VARCHAR2(15) Type NUMBER(5)

desc employee39
Name EMPNO EMPNAME DESIGNATION DATE_OF_JOIN SALARY EMPDNO Null? NOT NULL

NOT NULL

desc course39
Name COURSECODE COURSENAME Null? NOT NULL

desc student39
Name ROLLNO Null? NOT NULL

NAME COURSECODE MARK1 MARK2

VARCHAR2(15) NUMBER(2) NUMBER(3) NUMBER(3)

alter table employee39 add(phoneno number(10));
Table altered. desc employee39 Name EMPNO EMPNAME DESIGNATION DATE_OF_JOIN SALARY EMPDNO PHONENO Null? NOT NULL Type NUMBER(5) VARCHAR2(15) VARCHAR2(10) DATE NUMBER(9,2) NUMBER(2) NUMBER(10)

NOT NULL

alter table dept39 modify(dloc varchar2(15)); Table altered. desc dept39; Name Null? DNO NOT NULL DNAME DLOC select * from tab TNAME COURSE39 DEPT39 EMPLOYEE39 STUDENT39

Type NUMBER(2) VARCHAR2(15) VARCHAR2(15)

TABTYPE TABLE TABLE TABLE TABLE

CLUSTERID

insert into course39 values(75,'IT'); insert into course39 values(72,'ECE'); 1 row created. 1 row created. insert into student39 values(75001,'Sathya',75,89,98); insert into student39 values(75002,'Ramya',75,90,87); insert into student39 values(72028,'Shabana',72,97,85); 1 row created. 1 row created. 1 row created.

select * from student39;

ROLLNO NAME 75001 Sathya 75002 Ramya 72028 Shabana

COURSECODE 75 75 72

MARK1 89 90 97

MARK2 98 87 85

truncate table student39;
Table truncated.

select * from student39; no rows selected drop table student39;
Table dropped.

rename employee39 to emp39;
Table renamed.

EX: 2:… DML Commands..
1. insert into dept39 values(01,'sales','blocka');
1 row created.

2. alter table emp39 add(grade varchar2(1))
Table altered. insert into emp39 values(&empno,'&empname','&designation','&date_of_join',&salary,&depno,NULL,'&grade'); 3. select * from dept39; DNO 1 sales 2 purchase 3 production 4 marketing 5 accounts 6 software 6 rows selected.

DNAME blocka blockd blockb blocka blockc blocke

DLOC

select * from emp39; EMPNO EMPNAME DESIGNATIO DATE_OF_J SALARY EMPDNO PHONENO 1 Siva 2 Mani 3 Raju 4 Babu 5 Ram 6 Velu Manager salesman Clerk Clerk Salesman Programmer 15-OCT-87 12-APR-87 13-NOV-89 04-JAN-95 08-DEC-00 24-DEC-02 15000 5000 7000 5000 3000.25 10000 5 1 2 3 1 3 A F E E F A G

7 Ravi 8 Balan 9 Mahesh 10 Kumar 10 rows selected.

Accountant Manager Officer Analyst

12-SEP-91 07-JUN-93 18-MAR-97 15-JAN-95

8000 12000.75 10000.5 14500

5 3 2 6

E A B C

4. update emp39 set phoneno=240397 where empno=2;
1 row updated. select * from emp39; EMPNO EMPNAME 1 Siva 2 Mani 3 Raju 4 Babu 5 Ram 6 Velu 7 Ravi 8 Balan 9 Mahesh 10 Kumar 10 rows selected. DESIGNATIO Manager salesman Clerk Clerk Salesman Programmer Accountant Manager Officer Analyst DATE_OF_J SALARY EMPDNO PHONENO G 15-OCT-87 15000 5 240830 A 12-APR-87 5000 1 240397 F 13-NOV-89 7000 2 240053 E 04-JAN-95 5000 3 E 08-DEC-00 3000.25 1 240608 F 24-DEC-02 10000 3 A 12-SEP-91 8000 5 252107 E 07-JUN-93 12000.75 3 A 18-MAR-97 10000.5 2 B 15-JAN-95 14500 6 259585 C

5.delete from emp39 where date_of_join<’01_Jan_1989’;
2 rows deleted. 6.create table emp as select * from emp39; Table created. 7.select empname,designation,trunc(months_between(sysdate,date_of_join)/12)years_of_service from emp39; EMPNAME Raju Babu Ram Velu Ravi Balan Mahesh Kumar DESIGNATIO Clerk Clerk Salesman Programmer Accountant Manager Officer Analyst YEARS_OF_SERVICE 19 14 8 6 17 16 12 14

8 rows selected. 8. create view empview as select empname,designation,dname from emp39,dept39 where emp39.empdno=dept39.dno; View created. 9. select empname,salary from emp39 where salary=10000.50; EMPNAME Mahesh 10. select empname,empdno from emp39 where empdno<>01; EMPNAME Raju Babu Velu Ravi Balan Mahesh Kumar 11. select empname,date_of_join from emp39 where date_of_join>'04-Apr-1995'; EMPNAME Ram Velu Mahesh 08-DEC-00 24-DEC-02 18-MAR-97 DATE_OF_J EMPDNO 2 3 3 5 3 2 6 SALARY 10000.5

12. select empname,empdno from emp39 where empdno in(2,3,5); EMPNAME Raju Babu Velu Ravi Balan Mahesh 6 rows selected. 13. select empname,date_of_join from emp39 where date_of_join between '01-jan-1988' and '01jan-1998'; EMPNAME Raju 13-NOV-89 DATE_OF_J EMPDNO 2 3 3 5 3 2

Babu Ravi Balan Mahesh Kumar 6 rows selected.

04-JAN-95 12-SEP-91 07-JUN-93 18-MAR-97 15-JAN-95

14. select empname from emp39 where empname like 'R%'; EMPNAME Raju Ram Ravi 16 select empname,phoneno from emp39 where phoneno is NULL; EMPNAME Babu Velu Balan Mahesh 16 select empname,empdno,salary from emp39 where empdno=01 and salary=3000.25; EMPNAME Ram EMPDNO 1 SALARY 3000.25 PHONENO

17select empname,empdno,salary from emp39 where empdno=01 or salary=5000; EMPNAME Babu Ram EMPDNO 3 1 SALARY 5000 3000.25

18 select empname,designation from emp39 where designation NOT in('Manager','Officer'); EMPNAME Raju Babu Ram Velu Ravi Kumar 6 rows selected. 19. select * from dept39 order by dname; Clerk Clerk Salesman Programmer Accountant Analyst DESIGNATIO

DNO 5 accounts 4 marketing 3 production 2 purchase 1 sales 6 software 6 rows selected. EX. No. 3:

DNAME blockc blocka blockb blockd blocka blocke

DLOC

1. select designation,length(designation) from emp39 DESIGNATIO Clerk Clerk Salesman Programmer Accountant Manager Officer Analyst 8 rows selected. 2. select dname,substr(dname,1,3) from dept39 DNAME sales purchase production marketing accounts software 6 rows selected. sal pur pro mar acc sof SUB LENGTH(DESIGNATION) 5 5 8 10 10 7 7 7

3. select dloc,replace(dloc,'blocka','blockf') from dept39 DLOC blocka blockd blockb blocka REPLACE(DLOC,'BLOCKA','BLOCKF') blockf blockd blockb blockf

blockc blocke 6 rows selected.

blockc blocke

4. select rtrim('ABC0000','0'),ltrim('000ABC','0') from dual RTR ABC 5. select distinct designation from emp39 DESIGNATIO Accountant Analyst Clerk Manager Officer Programmer Salesman 7 rows selected. 6. update dept39 set dname='PRODUCTION' where dname='production'; update dept39 set dname='SALES' where dname='sales' 1 row updated. 1 row updated. select dname,lower(dname)from dept39 DNAME SALES purchase PRODUCTION marketing accounts software 6 rows selected. 7. select dloc,upper(dloc) from dept39 DLOC blocka BLOCKA UPPER(DLOC) sales purchase production marketing accounts software LOWER(DNAME) ABC LTR

blockd blockb blocka blockc blocke 6 rows selected.

BLOCKD BLOCKB BLOCKA BLOCKC BLOCKE

8. select grade,decode(grade,'A','manager','F','salesman','E','clerk','D','programmer','B','officer','C','anal yst') from emp39 G E E F A E A B C clerk clerk salesman manager clerk manager officer analyst DECODE(GRA

8 rows selected. 9. select count(empno) from emp39 COUNT(EMPNO) 8 10. select empdno,max(salary),min(salary) from emp39 group by empdno EMPDNO 1 2 3 5 6 MAX(SALARY) 3000.25 10000.5 12000.75 8000 14500 MIN(SALARY) 3000.25 7000 5000 8000 14500

11. select sum(salary) from emp39 SUM(SALARY) 69501.5

12. select avg(salary) from emp39 AVG(SALARY) 8687.6875 13. select salary,round(salary) from emp39 SALARY 7000 5000 3000.25 10000 8000 12000.75 10000.5 14500 8 rows selected. 14. select salary,floor(salary) from emp39 SALARY 7000 5000 3000.25 10000 8000 12000.75 10000.5 14500 8 rows selected. 15. select to_char(sysdate,'ddth month YYYY') from dual; TO_CHAR(SYSDATE,'DD 29th july 2009 FLOOR(SALARY) 7000 5000 3000 10000 8000 12000 10000 14500 ROUND(SALARY) 7000 5000 3000 10000 8000 12001 10001 14500

16. select to_date('20 sep 1996')date_format from dual DATE_FORM 20-SEP-96 17. select date_of_join,add_months(date_of_join,2)month_after from emp39

DATE_OF_J 13-NOV-89 04-JAN-95 08-DEC-00 24-DEC-02 12-SEP-91 07-JUN-93 18-MAR-97 15-JAN-95 8 rows selected. 13-JAN-90 04-MAR-95 08-FEB-01 24-FEB-03 12-NOV-91 07-AUG-93 18-MAY-97 15-MAR-95

MONTH_AFT

18. select date_of_join,last_day(date_of_join)last_day from emp39 DATE_OF_J 13-NOV-89 04-JAN-95 08-DEC-00 24-DEC-02 12-SEP-91 07-JUN-93 18-MAR-97 15-JAN-95 8 rows selected. 19. select trunc(months_between(sysdate,date_of_join))diff from emp39 DIFF 236 174 103 79 214 193 148 174 8 rows selected. 20. select next_day(sysdate,'FRIDAY')nextday from dual NEXTDAY 31-JUL-09 21.select to_char(to_date('1 January 2000','ddth month YYYY'),’day’)day from dual 30-NOV-89 31-JAN-95 31-DEC-00 31-DEC-02 30-SEP-91 30-JUN-93 31-MAR-97 31-JAN-95 LAST_DAY

DAY saturday

Ex. No: 4:
JOINS 1. select empname,dname from emp39,dept39 where emp39.empdno=dept39.dno

EMPNAME Raju Babu Ram Velu Ravi Balan Mahesh Kumar 8 rows selected. purchase PRODUCTION SALES PRODUCTION accounts PRODUCTION purchase software

DNAME

2. select x.empname,x.salary from emp39 x,emp39 y where x.salary>y.salary and y.empname='Raju' EMPNAME Velu Ravi Balan Mahesh Kumar SALARY 10000 8000 12000.75 10000.5 14500

3.select empname,dname from emp39,dept39 where emp39.empdno(+)=dept39.dno EMPNAME Ram Raju Mahesh Babu Balan Velu Ravi Kumar 9 rows selected. SALES purchase purchase PRODUCTION PRODUCTION PRODUCTION marketing accounts software DNAME

4. select empname from emp39 where empdno in(select dno from dept39 where dname in('PRODUCTION','SALES','software')); EMPNAME Babu Ram Velu Balan Kumar

SET OPERATIONS: 1. create table produ1(itemcode varchar2(5),itemname varchar2(10)); create table produ2(itemcode varchar2(5),itemname varchar2(10));

Table created. Table created. insert into produ1 values('&itemcode','&itemname'); insert into produ2 values('&itemcode','&itemname');
select itemname from produ1 union select itemname from produ2

ITEMNAME eraser notebook pen pencil ruler sketch pen textbook 7 rows selected. 2. select itemname from produ1 union all select itemname from produ2 ITEMNAME pen pencil ruler eraser sketch pen eraser notebook

textbook pen pencil 10 rows selected. 3. select itemname from produ1 intersect select itemname from produ2 ITEMNAME pen pencil ruler eraser sketch pen eraser notebook textbook pen pencil 10 rows selected. 4. select itemname from produ1 minus select itemname from produ2 ITEMNAME ruler sketch pen SUB-QUERIES: 1. select empname,salary from emp39 where salary=(select max(salary) from emp39); EMPNAME SALARY Kumar 14500 2. select empname,salary from emp39 where salary>any(select avg(salary) from emp39 group by empdno); EMPNAME Raju Babu Velu Ravi Balan Mahesh Kumar 7 rows selected. SALARY 7000 5000 10000 8000 12000.75 10000.5 14500

3. insert into emp39 values(0011,'Ramesh','Manager','6-Sep-1989',2500.50,02,230948,'A'); insert into emp39 values(0012,'Raja','Clerk','17-Nov-1989',3000,02,248390,'E'); 1 row created. 1 row created. select empname,salary from emp39 where salary<all(select avg(salary) from emp39 group by empdno); EMPNAME Ramesh Raja SALARY 2500.5 3000

4. select dname from dept39 where exists(select empdno from emp39 where emp39.empdno=dept39.dno) DNAME SALES purchase PRODUCTION accounts software 5. select * from emp39 where empdno=(select dno from dept39 where dname='software'); EMPNO EMPNAME DESIGNATIO DATE_OF_J SALARY EMPDNO PHONENO G 10 Kumar Analyst 15-JAN-95 14500 6 259585 C 6. select designation,empdno,count(*)no_of_employees from emp39 group by designation,empdno having count(*)=2 order by 2; DESIGNATIO Salesman Clerk Accountant EMPDNO 1 2 5 NO_OF_EMP 2 2 2

Sign up to vote on this title
UsefulNot useful