Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
141 views

Oracle Commands

The document contains SQL statements for creating tables, inserting and modifying data, joining tables, aggregating data, and more. It covers concepts such as data definition, data manipulation, data retrieval, constraints, indexes, sequences, functions and operators, and data types. The numerous SQL statements are grouped into 18 sections and cover a wide range of fundamental SQL topics.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
141 views

Oracle Commands

The document contains SQL statements for creating tables, inserting and modifying data, joining tables, aggregating data, and more. It covers concepts such as data definition, data manipulation, data retrieval, constraints, indexes, sequences, functions and operators, and data types. The numerous SQL statements are grouped into 18 sections and cover a wide range of fundamental SQL topics.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

SECTION 6

1). CREATE TABLE employ(ENO NUMBER(3),ENAME VARCHAR2(15),JOB VARCHAR2(15),SAL


NUMBER(8,2),DNO NUMBER(2));

2). CREATE TABLE newemploy AS SELECT * FROM employ;

3). DESC employ

4). INSERT INTO employ VALUES(100,’KUMAR’,’CLERK’,2000,10);

5). INSERT INTO employ(ENO,ENAME) VALUES(101,’RAVI’);

6). INSERT INTO employ VALUES(&ENO,’&ENAME’,’&JOB’,&SAL,&DNO);

7). INSERT INTO newemploy SELECT * FROM employ;

SECTION 7

8). Select * from employ;

9). Select eno,ename from employ;

10). Select * from emp where deptno=10;

11). Select deptno,sum(sal) from emp group by deptno;

12). Select deptno,sum(sal) from emp group by deptno having deptno>=20;

13). Select * from emp order by empno;

14). Select * from emp order by empno desc;

15). Update employ set sal=3000 where eno=100;

16). Update employ set sal=(select sal from employ where eno=100) where eno=101;

SECTION 8
17). Delete from employ where eno=101;

18). Truncate table newemploy;

19). Comment on column employ.ename is ‘this is the field employ name’

20). Rename newemploy to employ2;

21). Drop table employ2;

SECTION 9

22). Alter table employ add (doj date); -> to add a field.

23). Alter table employ modify (ename varchar(20));

24). Alter table employ drop column doj;

25). connect student/cat;

26). Grant all on emp to student;

27). Connect student/cat;

28). Select * from scott.emp;

29). Insert into scott.emp........;

30). grant select,insert on dept to student with grant option;

31). revoke all on emp from student; ( to get back all the given permissions).

SECTION 10

32). CREATE TABLE bonuses (employee_id NUMBER, bonus NUMBER DEFAULT 100);
33). INSERT INTO bonuses(employee_id) (SELECT e.employee_id FROM employees e, orders o

34). WHERE e.employee_id = o.sales_rep_id GROUP BY e.employee_id);

35). SELECT * FROM bonuses;

36). MERGE INTO bonuses D

USING (SELECT employee_id, salary, department_id FROM employees

WHERE department_id = 80) S ON (D.employee_id = S.employee_id)

WHEN MATCHED THEN UPDATE SET D.bonus = D.bonus + S.salary*.01

WHEN NOT MATCHED THEN INSERT (D.employee_id, D.bonus) VALUES (S.employee_id,


S.salary*0.1);

EMPLOYEE_ID BONUS

----------- ----------

153 180

154 175

155 170

156 200

158 190

159 180

160 175

161 170

163 195

157 950

145 1400

170 960

179 620

152 900
169 1000

...

37). EXPLAIN PLAN SET STATEMENT_ID = 'Raise in Tokyo' INTO plan_table FOR UPDATE employees SET
salary = salary * 1.10 WHERE department_id

= (SELECT department_id FROM departments WHERE location_id = 1200);

38). LOCK TABLE employees IN EXCLUSIVE MODE NOWAIT;

SECTION 11

40). savepoint a;

Insert into emp values.....;

Savepoint b;

Update emp set ename=’raj’ ....;

Select * from emp;

Rollback to savepoint b;

Select * from emp;

Rollback to savepoint a;

Select * from emp;

SECTION 12
41)

Select rownum ,rowid ,empno from emp;

creating hierarchical tree structure of data create table tree(eno number(3),ename varchar2(20),mgr
number(3));

insert the given data

ENO ENAME MGR

100 KING -

101 GOPI 100

102 SONY 100

103 RAJU 102

104 KUMAR 102

105 GEETHA 104

106 RAVI 105

107 JANI 105

select level ,eno,ename,mgr from tree start with eno=100 connect by prior eno=mgr order by level;

42)

Create sequence seq1

Start with 1

Increment by 1

Maxvalue 10
Minvalue 1

Nocycle

Cache 2;

Select seq1.nextval from dual;

Select seq1.nextval from dual;

Select seq1. currval from dual;

insert into emp(empno) values(seq1.nextval) ;

Select empno from emp;

SECTION 13

43).

SELECT 'Name is ' || ename FROM emp

SELECT * FROM emp WHERE sal = 1500;

SELECT * FROM emp WHERE sal != 1500;

SELECT * FROM emp WHERE sal > 1500;

SELECT * FROM emp WHERE sal < 1500;

SELECT * FROM emp WHERE sal >= 1500;


SELECT * FROM emp WHERE job IN ('CLERK','ANALYST');

SELECT * FROM emp WHERE job NOT IN('CLERK', ANALYST');

SELECT * FROM emp WHERE sal = ANY(SELECT sal FROM emp WHERE deptno = 30)

SELECT * FROM emp WHERE sal >= ALL ( 1400, 3000);

SELECT * FROM emp WHERE sal BETWEEN 2000 AND 3000);

SELECT dname, deptno FROM dept WHERE EXISTS (SELECT * FROM emp WHERE dept.deptno =
emp.deptno);

select * from emp where job like ‘C%’;

SELECT dname, deptno FROM emp WHERE comm IS NULL.

SELECT 'TRUE' FROM emp WHERE deptno NOT IN (5,15)

SECTION 14

44).

SELECT * FROM emp WHERE NOT (job IS NULL);

SELECT * FROM emp WHERE NOT (sal BETWEEN 1000 AND 2000);

SELECT * FROM emp WHERE job = 'CLERK' AND deptno = 10;

SELECT * FROM emp WHERE job = 'CLERK' OR deptno = 10

Create table empdummy as select * from emp where job=’CLERK’;

Select * from emp

Union
Select * from empdummy;

Select * from emp

Intersect

Select * from empdummy;

Select * from emp

Minus

Select * from empdummy;

Select * from emp

Union all

Select * from empdummy;

SECTION 15

45).

SELECT ABS(-15) "Absolute" FROM DUAL;

SELECT CEIL(15.7) "Ceiling" FROM DUAL;

SELECT COS(180 * 3.14159265359/180) "Cosine of 180 degrees" FROM DUAL ;


SELECT COSH(0) "Hyperbolic cosine of 0" FROM DUAL

SELECT EXP(4) "e to the 4th power" FROM DUAL;

SELECT FLOOR(15.7) "Floor" FROM DUAL;

SELECT LN(95) "Natural log of 95" FROM DUAL;

SELECT LOG(10,100) "Log base 10 of 100" FROM DUAL ;

SELECT MOD(11,4) "Modulus" FROM DUAL;

SELECT POWER(3,2) "Raised" FROM DUAL ;

SELECT ROUND(15.193,1) "Round" FROM DUAL ;

SELECT ROUND(15.193,-1) "Round" FROM DUAL;

SELECT SIGN(-15) "Sign" FROM DUAL;

SELECT SIN(30 * 3.14159265359/180) "Sine of 30 degrees" FROM DUAL ;

SELECT SINH(1) "Hyperbolic sine of 1" FROM DUAL;

SELECT SQRT(26) "Square root" FROM DUAL;

SELECT TAN(135 * 3.14159265359/180) "Tangent of 135 degrees" FROM DUAL ;

SELECT TANH(.5) "Hyperbolic tangent of .5" FROM DUAL ;

SELECT TRUNC(15.79,1) "Truncate" FROM DUAL ;

SELECT TRUNC(15.79,-1) "Truncate" FROM DUAL ;

SELECT CHR(67)||CHR(65)||CHR(84) "Dog" FROM DUAL

SELECT CONCAT( CONCAT(ename, ' is a '), job) "Job" FROM emp WHERE empno = 7900

SELECT INITCAP('the soap') "Capitals" FROM DUAL


SECTION 16

46).

SELECT LOWER('MR. SAMUEL HILLHOUSE') "Lowercase" FROM DUAL;

SELECT LPAD('Page 1',15,'*.') "LPAD example" FROM DUAL;

SELECT LTRIM('xyxXxyLAST WORD','xy') "LTRIM example" FROM DUAL;

SELECT REPLACE('JACK and JUE','J','BL') "Changes" FROM DUAL;

SELECT RPAD(ename,12,'ab') "RPAD example" FROM emp WHERE ename = 'TURNER' ;

SELECT RTRIM('TURNERyxXxy','xy') "RTRIM e.g." FROM DUAL;

SELECT ename FROM emp WHERE SOUNDEX(ename) = SOUNDEX('SMYTHE');

SELECT SUBSTR('ABCDEFG',3.1,4) "Subs" FROM DUAL;

SELECT TRANSLATE('2KRW229', '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',

'9999999999XXXXXXXXXXXXXXXXXXXXXXXXXX') "Licence" FROM DUAL;

SELECT UPPER('Large') "Uppercase" FROM DUAL;

SELECT ASCII('A') FROM DUAL;

SELECT INSTR('CORPORATE FLOOR','OR', 3, 2) "Instring" FROM DUAL;

SELECT LENGTH('CANDIDE') "Length in characters" FROM DUAL;

SECTION 17

47).
SELECT TO_CHAR(ADD_MONTHS(hiredate,1),'DD-MON-YYYY') "Next month" FROM emp WHERE ename
= 'SMITH';

SELECT SYSDATE,LAST_DAY(SYSDATE) "Last", LAST_DAY(SYSDATE) - SYSDATE "Days Left" FROM DUAL;

SELECT MONTHS_BETWEEN(TO_DATE('02-02-1995','MM-DD-YYYY'), TO_DATE('01-01-1995','MM-DD-


YYYY') ) "Months" FROM DUAL;

SELECT NEXT_DAY('15-MAR-92','TUESDAY') "NEXT DAY" FROM DUAL;

SELECT TO_CHAR(SYSDATE, 'MM-DD-YYYY HH24:MI:SS') NOW FROM DUAL;

SELECT ename FROM emp WHERE ROWID = CHARTOROWID('0000000F.0003.0002');

SELECT CONVERT('Groß', 'WE8HP', 'WE8DEC') "Conversion" FROM DUAL;

INSERT INTO graphics (raw_column) SELECT HEXTORAW('7D') FROM DUAL;

SELECT RAWTOHEX(raw_column) "Graphics" FROM graphics;

SELECT ROWID FROM graphics WHERE ROWIDTOCHAR(ROWID) LIKE '%F38%' ;

SELECT TO_CHAR(HIREDATE, 'Month DD, YYYY') "New date format" FROM emp WHERE ename = 'SMITH'
;

SELECT TO_CHAR(-10000,'L99G999D99MI') "Amount" FROM DUAL;

SELECT TO_CHAR(-10000,'L99G999D99MI', 'NLS_NUMERIC_CHARACTERS = '',.'' NLS_CURRENCY


= ''AusDollars'' ') "Amount" FROM DUAL;

INSERT INTO bonus (bonus_date) SELECT TO_DATE('January 15, 1989, 11:00 A.M.','Month dd,
YYYY, HH:MI A.M.','NLS_DATE_LANGUAGE = American') FROM DUAL;

UPDATE emp SET sal = sal + TO_NUMBER('100.00', '9G999D99') WHERE ename = 'BLAKE';
SELECT GREATEST('HARRY','HARRIOT','HAROLD') "GREATEST" FROM DUAL;

SELECT LEAST('HARRY','HARRIOT','HAROLD') "LEAST" FROM DUAL ;

SELECT ename, NVL(TO_CHAR(COMM),'NOT APPLICABLE') "COMMISSION" FROM emp WHERE deptno =


30 ;

SELECT USER, UID FROM DUAL ;

SELECT AVG(sal) "Average" FROM emp;

SELECT COUNT(*) "Total" FROM emp;

SELECT COUNT(job) "Count" FROM emp;

SELECT MAX(sal) "Maximum" FROM emp;

SELECT MIN(hiredate) "Minimum Date" FROM emp;

SELECT STDDEV(sal) "Deviation" FROM emp;

SELECT SUM(sal) "Total" FROM emp;

SELECT VARIANCE(sal) "Variance" FROM emp;

SECTION 18

48).

create table empl (empno number(5) not null,ename varchar2(15));


alter table empl modify (ename varchar2(15) not null);

Alter table empl modify (ename varchar2(15) null);

49). Create table empl (empno number(5) , ename varchar2(15) , doj date default sysdate );

50). CREATE TABLE dept1(deptno NUMBER CONSTRAINT check_deptno

CHECK (deptno BETWEEN 10 AND 99), dname VARCHAR2(9) CONSTRAINT


check_dname

CHECK (dname = UPPER(dname)) , loc VARCHAR2(10) CONSTRAINT check_loc

CHECK (loc IN ('DALLAS','BOSTON','NEW YORK','CHICAGO')) );

SECTION 19).

51).

CREATE TABLE dept1 (deptno NUMBER(2) CONSTRAINT unq_dname UNIQUE ,

dname VARCHAR2(9),loc VARCHAR2(10));

alter table dept1 add constraint unq1 unique(dname);

alter table dept1 drop constraint unq1;


CREATE TABLE dept1(deptno NUMBER(2), dname VARCHAR2(9),

loc VARCHAR2(10),CONSTRAINT unq_dname UNIQUE(deptno,dname));

52). CREATE TABLE dept1(deptno NUMBER(2) CONSTRAINT pk_deptx PRIMARY KEY,

dname VARCHAR2(9),loc VARCHAR2(10));

alter table dept1 add constraint pk_d primary key(dname);

alter table dept1 drop constraint pk_d;

CREATE TABLE dept1(deptno NUMBER(2),dname VARCHAR2(9),loc VARCHAR2(10),

CONSTRAINT pk_depty PRIMARY KEY(deptno,dname));

SECTION 20).

53).

CREATE TABLE dept1 (deptno NUMBER(3) CONSTRAINT pk_deptz PRIMARY KEY,

dname VARCHAR2(9),loc VARCHAR2(10));

CREATE TABLE emp1(empno NUMBER(4),ename VARCHAR2(10),job VARCHAR2(9), Mgr


NUMBER(4),

hiredate DATE,sal NUMBER(7,2),comm NUMBER(7,2),deptno NUMBER(3),

CONSTRAINT fk_deptnox FOREIGN KEY(deptno) REFERENCES dept1(deptno) );


alter table emp1 drop constraint fk_deptnox;

alter table emp1 add constraint fk_deptnox Foreign key(deptno) references

dept1(deptno));

CREATE TABLE emp1 (empno NUMBER(4), ename VARCHAR2(10), job VARCHAR2(9),

Mgr NUMBER(4), hiredate DATE, sal NUMBER(7,2), comm NUMBER(7,2),

Deptno NUMBER(2) CONSTRAINT fk_deptnoy REFERENCES dept1(deptno)

ON DELETE CASCADE);

Drop table dept1 cascade constraints.

SECTION 21).

54).

edit a

select * from emp;

@a

@@a
accept x

define

select * from emp;

append where sal>=2000;

select * from emp;

change /emp/dept

input where deptno=10;

list

list 1

list 2
SECTION 22).

55).

define

define no=10

define

create table x(n number(2));

insert into x values(&no);

select * from x;

undefine no;

define
/

del

edit x

......type some commands and close

get x

host dir/p

host date

Whenever sqlerror exit

select * from sdjfl; (invalid table name)

SECTION 23).

56).
Ttitle center 'This is employee Report'

Btitle left 'This is end of report'

Select * from emp;

Ttitle off

Btitle off

Select * from emp;

Ttitle on

Btitle on

Compute sum of sal on deptno

break on deptno

select * from emp order by deptno;

Column ename Heading 'Employ Name'

Column sal format '$999,9.99'

Select * from emp order by deptno;

Column ename clear

Column sal clear

Select * from emp order by deptno;

Clear breaks

Clear Col
Select * from emp;

Connect student/cat

select * from emp;

disconnect

select * from emp;

connect scott/tiger;

select * from emp;

Pause 'Press any key'

edit a

select * from emp;

pause 'press a key'

select * from dept;

@a

variable i number

Begin

:i: =10;

end;

print I
prompt 'This is sql*plus command'

Spool on

Spool xyz.txt

desc emp

select * from emp;

select * from dept;

spool off

edit xyz.txt

select * from dept;

save x

edit x

SECTION 24).

57).
set autocommit on

set autocommit 3000

set autotrace on

set autotrace explain

begin

Dbms_output.put_line(' this is oracle class');

End;

set blockterminator ' * '

begin

dbms_output.put_line('this is oracle class');

end;

/
set cmdsep on

Desc emp ; desc dept

Set cmdsep ','

Desc emp , desc dept

set colsep '******* '

Select * from dept;

define no=10;

Set concat '+'

Set concat on

Select * from dept where deptno=&no+1

define no=10

Select * from emp where deptno=&no;

Set define ' + '

Select * from emp where deptno=+no;

Set define off


edit a

Select * from emp;

Select * from dept;

Set echo on

Start a

Set echo off

Start a

set Edit file a

edit

set embedded on

Ttitle 'this is employs report'

Btitle 'end of report'

Select * from emp;

set escape '^'

Define no = 20

Select * from emp where deptno=&no;

Select * from emp where deptno=^&no;


set feedback 5

Select * from emp;

Set feedback 20

Select * from emp;

select * from emp;

Set heading off

Select * from emp;

Set heading on

Select * from emp

SECTION 25)

58).

column empno heading employnumber


Select * from emp;

Column empno heading employ|number

Select * from emp;

Set headsep '_'

Column empno heading employ _ number

Select * from emp;

select * from emp;

Set linesize 40

Select * from emp;

Set linesize 80

Select * from emp;

set maxdata 500

Select * from emp;

Set maxdata 5000

ttitle 'report of employs'

Btitle 'end of report'

Select * from emp;

Set newpage 10

Select * from emp;


select * from emp;

Set numformat '$ 99,999.99'

Select * from emp;

ttitle 'this is oracle class'

Btitle 'end of report'

Select * from emp;

Set pagesize 50

Select * from emp;

Set pagesize 8

Select * from emp;

set pause on

Select * from tab;

Set pause off

select * from emp;

Set recsep each

Select * from emp;


Set recsepchar ‘*’

Select * from emp;

Set recsep off

begin

Dbms_output.put_line('this is oracle class');

End;

set serveroutput on

desc –

>emp

set sqlcontinue '+'

desc –

+ emp

select * from emp;

Where deptno=20;
Set sqlnumber off

Select * from emp

Where deptno=20;

select * from emp;

# desc emp

set sqlprompt 'cat>'

Set sqlprompt 'sql>'

set sqlterminator '?'

Select * from emp?

set sqlterminator ';'

Select * from emp;

set suffix 'txt'

Edit f (enter any text)


/

set time on

Select * from emp;

Set time off

set timing on

Select * from emp;

Select * from dept;

Set timing off

set underline '*'

Select * from emp;

Set underline '-'

select * from distionary;

Set wrap on

Select * from dictionary;

Set wrap off


SECTION 26).

59).

Some sub-queries using EMP & DEPT tables:-

1.List details of employs who are working in RESEARCH department.

Select * from emp where deptno=(select deptno from dept where dname='RESEARCH');

2.List details of departments where the salesmans are working.

Select * from dept where deptno IN(select deptno from emp where job='SALESMAN');

3. List details of employs who are earning more than ‘ALLEN’;

Select * from emp where sal>(select sal from emp where ename='ALLEN');

4. Select second maximum salary of employs;

Select max(sal) from emp where sal>(select max(sal) from emp);

5. Update the employ’s commission whose commission is zero with the commission of ename ‘WARD’;

Update emp set comm=(select comm from emp where ename='WARD') where comm=0;
6. Get the count of employs who are earning more than the average salary of ACCOUNTING department.

Select count(*) from emp where sal>(select avg(sal) from emp where

deptno=(select deptno from dept where dname='ACCOUNTING');

CREATE TABLE DEPOSIT (ACTNO VARCHAR2(5) ,CNAME VARCHAR2(18) , BNAME VARCHAR2(18) ,

AMOUNT NUMBER(8,2) ,ADATE DATE);

CREATE TABLE BRANCH(BNAME VARCHAR2(18),CITY VARCHAR2(18));

CREATE TABLE CUSTOMERS(CNAME VARCHAR2(19) ,CITY VARCHAR2(18));

CREATE TABLE BORROW(LOANNO VARCHAR2(5), CNAME VARCHAR2(18),BNAME VARCHAR2(18),

AMOUNT NUMBER (8,2));

\
INSERT INTO DEPOSIT VALUES ('100','ANIL','VRCE',1000.00,'1-MAR-95');

INSERT INTO DEPOSIT VALUES ('101','SUNIL','AJNI',5000.00 ,'4-JAN-96');

INSERT INTO DEPOSIT VALUES ('102','MEHUL','KAROLBAGH',3500.00 ,'17-NOV-95' );

INSERT INTO DEPOSIT VALUES ('104','MADHURI','CHANDNI',1200.00 , '17-DEC-95' );

INSERT INTO DEPOSIT VALUES ('105','PRAMOD','M.G.ROAD',3000.00,'27-MAR-96');

INSERT INTO DEPOSIT VALUES ('106','SANDIP','ANDHERI',2000.00,'31-MAR-96');

INSERT INTO DEPOSIT VALUES ('107','SHIVANI','VIRAR',1000.00,'5-SEP-95');

INSERT INTO DEPOSIT VALUES ('108','KRANTI','NEHRU PLACE',5000.00,'2-JUL-95');

INSERT INTO DEPOSIT VALUES ('109','NAREN','POWAI',7000.00,'10-AUG-95');

INSERT INTO BRANCH VALUES ('VRCE','NAGPUR');

INSERT INTO BRANCH VALUES ('AJNI','NAGPUR');

INSERT INTO BRANCH VALUES ('KAROLBAGH','DELHI');

INSERT INTO BRANCH VALUES ('CHANDNI','DELHI');

INSERT INTO BRANCH VALUES ('DHARAMPETH','NAGPUR');

INSERT INTO BRANCH VALUES ('M.G.ROAD','BANGLORE');

INSERT INTO BRANCH VALUES ('ANDHERI','BOMBAY');

INSERT INTO BRANCH VALUES ('VIRAR','BOMBAY');

INSERT INTO BRANCH VALUES ('NEHRU PLACE','DELHI');

INSERT INTO BRANCH VALUES ('POWAI','BOMBAY');


INSERT INTO CUSTOMERS VALUES ('ANIL','CALCUTTA');

INSERT INTO CUSTOMERS VALUES ('SUNIL','DELHI');

INSERT INTO CUSTOMERS VALUES ('MEHUL','BARODA');

INSERT INTO CUSTOMERS VALUES ('MANDAR','PATNA');

INSERT INTO CUSTOMERS VALUES ('MADHURI','NAGPUR');

INSERT INTO CUSOTMERS VALUES ('PRAMOD','NAGPUR');

INSERT INTO CUSTOMERS VALUES ('SANDIP','SURAT');

INSERT INTO CUSTOMERS VALUES ('SHIVANI','BOMBAY');

INSERT INTO CUSTOMERS VALUES ('KRANTI','BOMBAY');

INSERT INTO CUSTOMERS VALUES ('NAREN','BOMBAY');

INSERT INTO BORROW VALUES ('201','ANIL','VRCE',1000.00);

INSERT INTO BORROW VALUES ('206','MEHUL','AJNI',5000.00);

INSERT INTO BORROW VALUES ('311','SUNIL','DHARAMPETH',3000.00);

INSERT INTO BORROW VALUES ('321','MADHURI','ANDHERI',2000.00);

INSERT INTO BORROW VALUES ('375','PRAMOD','VIRAR',8000.00);

INSERT INTO BORROW VALUES ('481','KRANTI','NEHRU PLACE',3000.00);

SECTION 27).
60).

1)GIVE NAME OF CUSTOMERS HAVING LIVING CITY BOMBAY AND BRANCH CITY NAGPUR.

SELECT D1.CNAME,D1.BNAME,C1.CNAME,C1.CITY,B1.CITY,B1.BNAME FROM DEPOSIT D1,CUSTOMERS


C1,

BRANCH B1 WHERE C1.CITY = 'BOMBAY' AND B1.CITY = 'NAGPUR' AND D1.CNAME = C1.CNAME AND
D1.BNAME = B1.BNAME;

2)GIVE NAME OF CUSTOMERS HAVING SAME LIVING CITY AS THEIR BRANCH CITY.

SELECT D1.CNAME,D1.BNAME,C1.CNAME,C1.CITY,B1.BNAME,B1.CITY FROM DEPOSIT D1,CUSTOMERS


C1,

BRANCH B1 WHERE C1.CITY =B1.CITY AND D1.CNAME = C1.CNAME AND D1.BNAME = B1.BNAME;

3)GIVE NAME OF CUSTOMER WHO ARE BORROWERS AND DEPOSITORS AND HAVING LIVING CITY
NAGPUR.

SELECT D1.CNAME C1.CNAME, C1.CITY, BR1.CNAME FROM CUSTOMERS C1, DEPOSIT D1,BORROW BR1

WHERE C1.CITY ='NAGPUR' AND C1.CNAME = D1.CNAME AND D1.CNAME = BR1.CNAME ;

4) GIVE NAME OF CUSTOMERS WHO ARE DEPOSITORS HAVING SAME BRANCH CITY OF MR. SUNIL.
SELECT D1.BNAME, B1.BNAME FROM DEPOSIT D1,BRANCH B1 WHERE D1.BNAME AND B2.CITY

IN (SELECT D2.CNAME, D2.BNAME FROM DEPOSIT D2,BRANCH B2 WHERE D2.CNAME = 'SUNIL'

AND D2.BNAME = B1.BNAME);

5) GIVE NAME OF DEPOSITORS HAVING SAME LIVING CITY AS MR. ANIL AND HAVING DEPOSIT AMOUNT

GREATER THAN 2000.

SELECT D1.CNAME,D1.AMOUNT,C1.CNAME, C1.CITY FROM DEPOSIT D1,CUSTOMERS C1 WHERE


D1.AMOUNT > 2000

AND D1.CNAME = C1.CNAME AND C1.CITY IN (SELECT C2.CITY FROM CUSTOMERS C2 WHERE C2.CNAME
= 'ANIL');

6) GIVE NAME OF BORROWERS HAVING DEPOSIT AMOUNT GREATER THAN 1000 AND LOAN AMOUNT
GREATER

THAN 2000.

SELECT BR1.CNAME, BR1.AMOUNT, D1.CNAME, D1.AMOUNT FROM BORROW BR1,DEPOSIT D1 WHERE

D1.CNAME = BR1.CNAME AND D1.AMOUNT > 1000 AND BR1.AMOUNT > 2000;

7)GIVE NAME OF DEPOSITORS HAVING SAME BRANCH AS BRANCH OF SUNIL.

SELECT D1.BNAME FROM DEPOSIT D1 WHERE D1.BNAME IN (SELECT D2.CNAME FROM DEPOSIT D2

WHERE D2.CNAME = 'SUNIL');

8)GIVE NAME OF BORROWERS HAVING LOAN AMOUNT GREATER THAN AMOUNT OF PRAMOD.
SELECT BR1.CNAME,BR1.AMOUNT FROM BORROW BR1 WHERE BR1.AMOUNT >

ALL (SELECT BR2.AMOUNT FROM BORROW BR2 WHERE BR2.CNAME = 'PRAMOD');

9)GIVE NAME OF CUSTOMERS LIVING IN SAME CITY WHERE BRANCH OF DEPOSITOR SUNIL IS LOCATED.

SELECT C1.CITY FROM CUSTOMERS C1 WHERE C1.CITY IN

(SELECT D1.BNAME,D1.CNAME,B1.BNAME FROM BRANCH B1 ,DEPOSIT D1 WHERE D1.CNAME

= 'SUNIL' AND D1.BNAME = B1.BNAME);

SELECT BR1.LOANNO,BR1.AMOUNT FROM BORROW BR1 WHERE

BR1.BNAME= (SELECT D1.BNAME FROM DEPOSIT D1 WHERE D1.CNAM='SUNIL');

SELECT D1.ACTNO , D1.CNAME,D1.BNAME ,D1.AMOUNT ,D1.ADATE , BR1.LOANNO , BR1.BNAME ,

BR1.AMOUNT FROM DEPOSIT D1 , BORROW BR1 , CUSTOMERS C1 WHERE C1.CNAME = D1.CNAME


AND D1.CNAME = BR1.CNAME AND C1.CITY IN (SELECT C2.CITY FROM CUSTOMERS C2 WHERE

C2.CNAME = 'PRAMOD');

SELECT D1.CNAME FROM DEPOSIT D1,CUSTOMERS C1,BRANCH B1 WHERE D1.CNAME = C1.CNAME AND

D1.BNAME = B1.BNAME AND B1.CITY IN (SELECT B2.CITY FROM BRANCH B2,DEPOSIT D2 WHERE

D2.CNAME = 'SUNIL'AND D2.BNAME = B2.BNAME) AND C1.CITY IN (SELECT C2.CITY FROM CUSTOMERS
C2

WHERE C2.CNAME = 'ANIL');

SELECT D1.CNAME FROM DEPOSIT D1 WHERE D1.BNAME IN (SELECT D2.BNAME FROM DEPOSIT D2
WHERE

D2.CNAME = 'SUNIL' );

SELECT D1.CNAME FROM DEPOSIT D1 WHERE D1.CNAME IN (SELECT C1.CNAME FROM CUSTOMERS C1
WHERE

C1.CITY = 'NAGPUR');
SECTION 28).

61).

select * from emp x where sal>(select avg(sal) from emp where x.deptno=deptno)

Select empno,ename,dname,loc from emp,dept;

select emp.empno,emp.ename,dept.deptno,dept.dname from emp,dept where

emp.deptno=dept.deptno;

Select Emp.Ename, Dept.Dname From Emp E, Dept D WHERE E.Deptno BETWEEN 10 AND 30;

select * from emp worker,emp manager where worker.mgr=manager.empno

and worker.sal>manager.sal;

select emp.empno,emp.ename,emp.deptno,dept.deptno,dept.dname from emp,dept where

emp.deptno=dept.deptno(+);

SECTION 29).

62).
create view v1 as select * from dept;

select * from v1;

desc v1

insert into v1 values(60,’personal’,’chennai’);

select * from dept;

drop view v1;

create view v1 as select emp.empno,emp.ename,dept.dname from emp,dept

where emp.deptno=dept.deptno;

select * from v1;

create force view v2 as select * from xyxyxy;

SECTION 30).

63).

Create public synonym syn1 for emp;

select * from syn1;

drop public synonym syn1;

create index idx1 on emp(empno);

drop index idx1;

Create cluster cl1 ( deptno number(3));


Create index idx2 on Cluster cl1;

SECTION 32).

64).

Declare

a number;

Begin

a:=&a;

dbms_output.put_line ('the number is'|| to_char(a));

End;

65).

Declare

A number;

B number;

C number;

Begin

A:=&A;
B:=&B;

C:=A+B;

dbms_output.put_line ('A='|| to_char(A) || ' B='||to_char(B) || 'C='||to_char(C));

End;

SECTION 33).

66).

declare

no number(3);

begin

no:=&no;

if mod(no,2)=0 then

dbms_output.put_line('The given no is even');

else

dbms_output.put_line('The given no is odd');

end if;

end;

67).

declare
i number;

begin

i:=1;

while i<=20 loop

dbms_output.put_line('the number is ..'|| to_char(i));

i:=i+1;

end loop;

end;

68).

declare

str varchar2(20);

l number:=0;

p number:=1;

begin

str:='&str';

while substr(str,p,1) is not null

loop

l:=l+1;

p:=p+1;

end loop;

dbms_output.put_line('The string length is...' || to_char(l));

end;
69).

declare

no number(2);

i number;

begin

no:=&no;

for i in 1..10

loop

dbms_output.put_line(to_char(i) || ' * '|| to_char(no)|| ' = ' || to_char(i*no));

end loop;

end;

SECTION 34)

70).

declare

no number;

work varchar(20);

name varchar(20);

begin

no:=&no;

select job,ename into work,name from emp where empno=no;


dbms_output.put_line(name ||' is working as ' || work);

end;

71).

declare

salary emp.sal%type;

no number;

begin

no:=&no;

select sal into salary from emp where empno=no;

if salary > 3000 then

update emp set sal=sal+100 where empno=no;

else

update emp set sal=sal+200 where empno=no;

end if;

commit;

end;

72).

Create table temp (empno number(4),sal number(7,2),grade char(1));

declare
no emp.empno%type;

esal emp.sal%type;

grad temp.grade%type;

begin

no:=&no;

select sal into esal from emp where empno=no;

if esal > 500 and esal<2000 then

grad:='c';

elsif esal>=2000 and esal<4000 then

grad:='b';

else

grad:='a';

end if;

insert into temp values(no,esal,grad);

commit;

end;

SECTION 35).

73).

Declare
no emp.empno%type;

sl emp.sal%type;

Begin

no:=&no;

select sal into sl from emp where empno=no;

update emp set sal=sl*1.1 where empno=no;

Exception

when no_data_found then

raise_application_error(-20001,'Number not found');

when too_many_rows then

raise_application_error(-20002,'Duplicate eno found');

when others then

raise_application_error(-20003,'Invalid input');

End;

74).

Declare

No number;

Sl number;

Invalid_sal exception;

Begin

No:=&no;

SELECT SAL INTO Sl FROM EMP WHERE empno=no;


If sl<8000 then

Raise invalid_sal;

Else

Update emp set sal=sl*1.20 WHERE empno=no;

End if;

Exception

When invalid_sal then

RAISE_APPLICATION_ERROR(-20001,'THE SAL IS less than expected');

When too_many_rows then

Raise_application_error(-20003,'query returned more than one row


='||to_char(sql%rowcount+1));

End;

SECTION 36).

75).

DECLARE

NAME EMP.ENAME%TYPE;

WORK EMP.JOB%TYPE;

CURSOR C1 IS SELECT ENAME,JOB FROM EMP;

BEGIN

OPEN C1;

LOOP
FETCH C1 INTO NAME,WORK;

EXIT WHEN C1%NOTFOUND;

DBMS_OUTPUT.PUT_LINE(NAME ||' IS WORKING AS ' ||WORK);

END LOOP;

CLOSE C1;

END;

76).

DECLARE

NO EMP.Empno%TYPE;

SL EMP.SAL%TYPE;

GRD TEMP.GRADE%TYPE;

ZERO_SAL EXCEPTION;

CURSOR C1 IS SELECT EMPNO,SAL FROM EMP;

BEGIN

DELETE FROM TEMP;

COMMIT;

OPEN C1;

LOOP

FETCH C1 INTO NO,SL;

EXIT WHEN C1%NOTFOUND;

IF SL=0 THEN

RAISE ZERO_SAL;
END IF;

IF SL>=1 AND SL<3000 THEN

GRD:='C';

ELSIF SL>=3000 AND SL<6000 THEN

GRD:='B';

ELSE

GRD:='A';

END IF;

INSERT INTO TEMP VALUES(NO,SL,GRD);

END LOOP;

COMMIT;

EXCEPTION

WHEN ZERO_SAL THEN

RAISE_APPLICATION_ERROR(-20001,'INVALID SAL FOUND');

END;

77).

declare

tsal number:=0;

cursor c1 is select empno,sal from emp;

e1 c1%rowtype;

begin

for e1 in c1 loop

tsal:=tsal+e1.sal;
dbms_output.put_line(to_char(e1.empno)||' '|| to_char(e1.sal));

end loop;

dbms_output.put_line('The total sal is ' ||to_char(tsal));

end;

SECTION 37).

78).

create or replace Procedure findname (eno IN number, ename1 out varchar2) is ename2
varchar2(30);

begin

select ename into ename2 from emp where empno = eno;

ename1 := ename2;

Exception

when no_data_found then

raise_application_error (-20100,'The given employ number is not present');

end;

79).

declare

eno number(5);

ename2 varchar2(30);
begin

eno := &eno;

findname(eno,ename2);

dbms_output.put_line(ename2);

end;

80).

declare

A number;

B number;

C number;

procedure sum (X IN number,Y IN number,Z OUT number) is

begin

Z := X+Y;

end;

begin

A:=5;

B:=10;

sum(A,B,C);

dbms_output.put_line (to_char(C));

end;
81).

create procedure p3(a in out number) is

begin

select deptno into a from emp where empno=a;

end;

82).

declare

x number;

begin

p3(x);

dbms_output.put_line('The deptno is ' || to_char(x));

end;

83).

create procedure p4(no in number) is

begin

delete from emp where empno=no;

commit;

end;

84). exec p4(100)


85).

create or replace function getname(eno number) return varchar2 as

ename1 varchar2(30);

begin

select ename into ename1 from emp where empno = eno;

return (ename1);

Exception

when no_data_found then

raise_application_error(-20110,'The employ number is not present');

end;

86).

declare

ename2 varchar2(30);

eno number(5);

begin

eno := &eno;

ename2 := getname(eno);

dbms_output.put_line(ename2);

end;

87). create function f2(no number) return number is


sl number;

begin

select sal into sl from emp where empno=no;

if sl>5000 then

return 1;

else

return 0;

end if;

end;

88). select f2(100) from dual;

SECTION 38).

89).

create package packg is

procedure delrec(no number);

procedure incrsal(no number);

end packg;
90).

create package body packg is

procedure delrec(no number) is

begin

delete from emp where empno=no;

commit;

end;

procedure incrsal(no number) is

b number;

begin

select sal into b from emp where empno=no;

if b<1000 then

b:=b*1.10;

else

b:=b*1.20;

end if;

update emp set sal=b where empno=no;

commit;

end;

end packg;
91).

exec packg.delrec ( 100 )

exec packg.incrsal ( 200 )

SECTION 39).

92).

CREATE OR REPLACE TRIGGER T1

BEFORE DELETE OR INSERT ON EMP

FOR EACH ROW

BEGIN

IF TO_CHAR(SYSDATE,'HH24')<9 OR TO_CHAR(SYSDATE,'HH24')>17 THEN

RAISE_APPLICATION_ERROR(-20001,'THIS IS NOT THE TIME TO DELETE');

END IF;

END;

93).

create or replace trigger t1

before update of sal on emp for each row


when (new.sal>=5000)

begin

:new.sal:=:new.sal+(:old.sal*0.01);

end;

94).

Create table emp1 as select * from emp where empno=0;

CREATE OR REPLACE TRIGGER trg2

after INSERT ON emp

FOR EACH ROW

BEGIN

insert into empl


values(:new.empno,:new.ename,:new.job,:new.mgr,:new.hiredate,:new.sal,:new.comm,:new.deptno);

END;

SECTION 40).

95).

CREATE TYPE PointType AS OBJECT (

x NUMBER,

y NUMBER

);
/

96).

CREATE TYPE LineType AS OBJECT (

end1 PointType,

end2 PointType

); /

97).

CREATE TABLE Lines (

lineID INT,

line LineType

);

98).

DROP TYPE Linetype;

99).

INSERT INTO Lines

VALUES(27, LineType(

PointType(0.0, 0.0),
PointType(3.0, 4.0)

);

SECTION 41).

100).

CREATE TYPE LineType AS OBJECT (

end1 PointType,

end2 PointType,

MEMBER FUNCTION length(scale IN NUMBER) RETURN NUMBER,

PRAGMA RESTRICT_REFERENCES(length, WNDS)

);

/
101).

CREATE TYPE BODY LineType AS

MEMBER FUNCTION length(scale NUMBER) RETURN NUMBER IS

BEGIN

RETURN scale *

SQRT((SELF.end1.x-SELF.end2.x)*(SELF.end1.x-SELF.end2.x) +

(SELF.end1.y-SELF.end2.y)*(SELF.end1.y-SELF.end2.y)

);

END;

END;

102). SELECT lineID, ll.line.length(2.0) FROM Lines ll;

SELECT ll.line.end1.x, ll.line.end1.y FROM Lines ll;

SELECT ll.line.end2 FROM Lines ll;

CREATE TABLE Lines1 OF LineType;

SELECT AVG(ll.length(1.0)) FROM Lines1 ll;

CREATE TABLE Lines2 ( end1 REF PointType, end2 REF PointType);


CREATE TABLE Points OF PointType;

INSERT INTO Lines2 SELECT REF(pp), REF(qq) FROM Points pp,Points qq WHERE pp.x < qq.x;

SELECT ll.end1.x, ll.end2.x FROM Lines2 ll;

CREATE TYPE PolygonType AS TABLE OF PointType;

CREATE TABLE Polygons (name VARCHAR2(20),points PolygonType) NESTED TABLE points


STORE AS PointsTable;

INSERT INTO Polygons VALUES( 'square', PolygonType(PointType(0.0, 0.0), PointType(0.0, 1.0),

PointType(1.0, 0.0), PointType(1.0, 1.0) ) );

SELECT points FROM Polygons WHERE name = 'square';

SELECT ss.x FROM THE(SELECT points FROM Polygons WHERE name = 'square' ) ss WHERE ss.x
= ss.y;

CREATE TYPE Tools_Va AS VARRAY(5) OF VARCHAR2(25);

CREATE TABLE Borrower ( name varchar2(25) , tools Tools_Va, Constraint B_pk


primary key( name));

desc Borrower

insert into Borrower values( 'Jack',Tools_Va('Hammer','Sledge','Ax'));

set serveroutput on

103). Declare

cursor B_cursor is select * from Borrower;

Begin

for B_rec in B_cursor

loop

dbms_output.put_line(' Contact name: ' || B_rec.name);

for I in 1..B_rec.Tools.count

loop

dbms_output.put_line(B_rec.Tools(I));

end loop;

end loop;

end;

/
SECTION 42).

104).

Create table PROPOSAL ( Proposal_Id number(10) , Recipient_Name varchar2(25) , Proposal_Name


varchar2(25),

Short_Description varchar2(1000) , Proposal_Text CLOB ,Bubget BLOB , Cover_Letter BFILE, constraint


Pr_Pk

Primary Key (Proposal_Id))

Insert into PROPOSAL (Proposal_Id , Recipient_Name , Proposal_Name, Short_Description


,Proposal_Text ,Bubget,

Cover_Letter ) VALUES (1,'DOT PHILLIPS','CLEAR PHILLIPS FIELD',NULL,'This is the text of a proposal to


clear

phillips field',EMPTY_BLOB(),NULL);

Insert into PROPOSAL (Proposal_Id , Recipient_Name , Proposal_Name, Short_Description ,

Proposal_Text ,Bubget, Cover_Letter ) VALUES (2,'BRAD OHMONT','REBUILD FENCE',

NULL,EMPTY_CLOB(),EMPTY_BLOB(),BFILENAME('proposal_dir' ,'p2.doc'));
105).

declare

locator_var CLOB;

amount_var INTEGER;

offset_var INTEGER;

output_var VARCHAR2(10);

begin

amount_var:=10;

offset_var:=1;

select proposal_text into locator_var from PROPOSAL where proposal_id=1;

DBMS_LOB.READ(locator_var, amount_var,offset_var,output_var);

DBMS_OUTPUT.PUT_LINE('Start of proposal text: ' || output_var);

end; /

106). declare

locator_var CLOB;

amount_var INTEGER;

offset_var INTEGER;

buffer_var VARCHAR2(25);

begin

amount_var:=12;

offset_var:=10;

buffer_var:=' ADD NEW TEXT ';


select proposal_text into locator_var from PROPOSAL where proposal_id=1 for update;

DBMS_LOB.WRITE(locator_var, amount_var,offset_var,buffer_var);

commit;

end; /

107). Select proposal_text from proposal where proposal_id=1;

You might also like