You are on page 1of 37

1.

Creation of a database and writing SQL queries to retrieve information from the
database.
CREATE COMMAND:
SQL> CREATE TABLE employee ( Employee_name varchar2(10),employee_no number(8),
dept_name varchar2(10),dept_no number (5),date_of_join date);
Table created.
TABLE DESCRIPTION
------------------------------SQL> desc employee;
Name
Null?
Type
------------------------------- -------- -----------------------EMPLOYEE_NAME
VARCHAR2(10)
EMPLOYEE_NO
NUMBER(8)
DEPT_NAME
VARCHAR2(10)
DEPT_NO
NUMBER(5)
DATE_OF_JOIN
DATE
INSERT COMMAND:
SQL> insert into employee values ('Vijay',345,'CSE',21,'21-jun-2006');
1 row created.
SQL>insert into employee (Employee_name,employee_no,dept_name,dept_no,date_of_join)
values ('Vishva',128, 'ECE',87,'25-dec-2006');
1 row created.
SELECT COMMAND:
SELECT Employee_name FROM employee;
SELECT Employee_name, dept_name FROM employee;
SELECT * FROM employee;

2. Performing Insertion, Deletion, Modifying, Altering, Updating and Viewing records


based on conditions.
ALTER
To add a column in a table
SQL> alter table employee add (salary number);
Table altered.
To delete a column in a table
SQL> alter table employee drop column salary;
UPDATE
SQL> update employee set salary = 100 where employee_no=98;
1 row updated.
MODIFY
SQL> alter table employee modify ( salary varchar2(10));
Table altered.
SELECT:
Select employee_name from employee where dept_name = CSE;
DELETE
SQL>delete from employee where employee_no = 344;
DROP TABLE
SYNTAX:
drop table employee;
Table dropped
TRUNCATE TABLE
SYNTAX:
truncate table employee;
Table truncated
RENAMING THE TABLE STUD
SQL> rename stud to studs;
Table renamed.
ARITHMETIC OPERATION
SQL> select sname, sid+100 "stid" from studs;
SNAME
stid
------------------------------ --------ashwin
201
bhavesh
202
pruthvik
203
charith
204
CONCATENATION OPERATOR
SQL> select sname || ' is a ' || sdept || ' engineer. ' AS "PROFESSION" from studs;
PROFESSION
------------------------------------------------------------------ashwin is a aeronautical engineer.
bhavesh is a marine engineer.
pruthvik is a aerospace engineer.
charith is a mechanical engineer.
2

DISPLAY ONLY DISTINCT VALUES


SQL> select distinct sarea from studs;
SAREA
-------------------anna nagar
kilpauk
nungambakkam
USING THE WHERE CLAUSE
SQL> select sname,sage from studs where sage<=19;
SNAME
SAGE
------------------------------ --------ashwin
19
bhavesh
18
BETWEEN OPERATOR
SQL> select sname,sarea, sid from studs where sid between 102 and 104;
SNAME
SAREA
SID
------------------------------ -------------------- ---------bhavesh
nungambakkam
102
pruthvik
anna nagar
103
charith
kilpauk
104
IN PREDICATE
SQL> select sname,sarea , sid from studs where sid in(102,104);
SNAME
SAREA
SID
------------------------------ -------------------- ---------bhavesh
nungambakkam
102
charith
kilpauk
104
PATTERN MATCHING
SQL> select sname, sarea from studs where sarea like '%g%';
SNAME
SAREA
------------------------------ -------------------ashwin
anna nagar
bhavesh
nungambakkam
pruthvik
anna nagar
LOGICAL AND OPERATOR
SQL> select sname ,sid from studs where sid>102 and sarea='anna nagar';
SNAME
SID
------------------------------ ---------pruthvik
103
LOGICAL OR OPERATOR
SQL> select sname ,sid from studs where sid>102 or sarea='anna nagar';
SNAME
SID
------------------------------ ---------ashwin
101
pruthvik
103
charith
104
3

NOT IN PREDICATE
SQL> select sname, sid from studs where sid not in(102,104);
SNAME
SID
------------------------------ ---------ashwin
101
pruthvik
103
UPDATING THE TABLE
SQL> alter table studs add ( spocket varchar2(20) );
Table altered.
SQL> update studs set spocket=750 where sid=101;
1 row updated.
SQL> update studs set spocket=500 where sid=102;
1 row updated.
SQL> update studs set spocket=250 where sid=103;
1 row updated.
SQL> update studs set spocket=100 where sid=104;
1 row updated.
SQL> select * from studs;
SNAME
SID
SAGE SAREA
SDEPT
------------------------------ ---------- --------- -------------------- -------------------SPOCKET
-------------------ashwin
101
19 anna nagar
aeronautical
750
bhavesh
102
18 nungambakkam
marine
500
pruthvik
103
20 anna nagar
aerospace
250
charith
104
20 kilpauk
mechanical
100
AGGREGATE FUNCTIONS
SQL> select avg( spocket ) result from studs;
RESULT
--------400
SQL> select min(spocket) result from studs;
RESULT
-------------------100
SQL> select count(spocket) result from studs;
RESULT
--------4
SQL> select count(*) result from studs;
RESULT
--------4
4

SQL> select count(spocket) result from studs where sarea='anna nagar';


RESULT
--------2
SQL> select max(spocket) result from studs;
RESULT
-------------------750
SQL> select sum(spocket) result from studs;
RESULT
--------1600
NUMERIC FUNCTIONS
SQL> select abs(-20) result from dual;
RESULT
--------20
SQL> select power (2,10) result from dual;
RESULT
--------1024
SQL> select round(15.359,2) result from dual;
RESULT
--------15.36
SQL> select sqrt (36) result from dual;
RESULT
--------6
STRING FUNCTIONS
SQL> select lower('ORACLE') result from dual;
RESULT
-----oracle
SQL> select upper('oracle') result from dual;
RESULT
-----ORACLE
SQL> select initcap('Oracle') result from dual;
RESULT
-----Oracle
SQL> select substr('oracle' ,2 ,5) result from dual;
RESULT
----racle
SQL> select lpad('oracle',10,'#') result from dual;

RESULT
---------####oracle
SQL> select rpad ('oracle',10,'^') result from dual;
RESULT
---------oracle^^^^
DATE FUNCTIONS
SQL> select sysdate from dual;
SYSDATE
--------16-JUL-08
SQL> select sysdate,add_months(sysdate,4) result from dual;
SYSDATE RESULT
----------------16-JUL-08 16-NOV-08
SQL> select sysdate, last_day(sysdate) result from dual;
SYSDATE RESULT
----------------16-JUL-08 31-JUL-08
SQL> select sysdate, next_day(sysdate,'sunday') result from dual;
SYSDATE RESULT
----------------16-JUL-08 20-JUL-08
SQL> select months_between('09-aug-91','11-mar-90') result from dual;
RESULT
--------16.935484
GROUP BY CLAUSE
SQL> select sarea, sum(spocket) result from studs group by sarea;
SAREA
RESULT
------------------------------anna nagar
1000
nungambakkam
500
kilpauk
100
HAVING CLAUSE
SQL> select sarea, sum(spocket) result from studs group by sarea having spocket<600;
SAREA
RESULT
------------------------------nungambakkam
500
kilpauk
100
DELETION
SQL> delete from studs where sid=101;
6

1 row deleted.
SQL> select * from studs;
SNAME
SID
SAGE
SAREA
SDEPT
SPOCKET
------------------------------ ---------- --------- -------------------- -------------------- ------------------bhavesh
102
18
nungambakkam
marine
500
pruthvik
103
20
anna nagar
aerospace
250
charith
104
20
kilpauk
mechanical
100
CREATING TABLES FOR DOING SET OPERATIONS
TO CREATE PRODUCT TABLE
SQL> create table product(prodname varchar2(30), prodno varchar2(10));
Table created.
SQL> select * from product;
PRODNAME
PRODNO
------------------------------ ---------table
10001
chair
10010
desk
10110
cot
11110
sofa
10010
tvstand
11010
TO CREATE SALE TABLE
SQL> create table sale(prodname varchar2(30),orderno number(10),prodno varchar2(10));
Table created.
SQL> select * from sale;
PRODNAME
ORDERNO PRODNO
----------------------------------------------table
801
10001
chair
805
10010
desk
809
10110
cot
813
11110
sofa
817
10010
SET OPERATIONS
SQL> select prodname from product where prodno=10010 union select prodname from sale
where prodno=10010;
PRODNAME
-----------------------------chair
sofa
SQL> select prodname from product where prodno=11110 intersect select prodname from sale
where prodno=11110;
PRODNAME
-----------------------------cot
7

SQL> select * from sstud1;


SNAME
PLACE
-------------------- -------------------prajan
chennai
anand
chennai
kumar
chennai
ravi
Chennai
SQL> select * from sstud2;
SNAME
DEPT
MARKS
-------------------- ---------- --------prajan
cse
700
anand
it
650
vasu
cse
680
ravi
it
600
NESTED QUERIES
SQL> select sname from sstud1 where sstud1.sname in ( select sstud2.sname from sstud2 );
SNAME
-------------------anand
prajan
ravi
SQL> select sname from sstud1 where sstud1.sname not in ( select sstud2.sname from sstud2 );
SNAME
-------------------kumar
SQL> select sname from sstud2 where marks > some(select marks from sstud2 where
dept='cse');
SNAME
-------------------prajan
SQL> select sname from sstud2 where marks >= some (select marks from sstud2 where
dept='cse' );
SNAME
-------------------prajan
vasu
SQL> select sname from sstud2 where marks > any ( select marks from sstud2 where
dept='cse' );
SNAME
-------------------prajan
SQL> select sname from sstud2 where marks >= any ( select marks from sstud2 where dept='cse'
);
SNAME
-------------------prajan
vasu
8

SQL> select sname from sstud2 where marks > all ( select marks from sstud2 where dept='cse' );
no rows selected
SQL> select sname from sstud2 where marks < all ( select marks from sstud2 where dept='cse' );
SNAME
-------------------anand
ravi
SQL> select sname from sstud1 where exists ( select sstud2.sname from sstud2 where
sstud1.sname=sstud2.sname );
SNAME
-------------------prajan
anand
ravi
SQL> select sname from sstud1 where not exists ( select sstud2.sname from sstud2 where
sstud1.sname=sstud2.sname );
SNAME
-------------------kumar

3. Creation of Views, Synonyms, Sequence, Indexes, Save point.


SQL> create table department(d_no number(10) primary key,
dept_name varchar2(10) not null,
manager varchar2(20));
Table created.
SQL> create table employee(emp_no number(10) primary key,
emp_name varchar2(20) not null,
dept_no number(10),
ph_no number(10),
foreign key (dept_no) references department (d_no));
Table created.
SQL> select * from employee;
EMP_NO EMP_NAME
PH_NO
---------- -------------------- ---------- ---------1 Abi
445566
101
2 Banu
586987
102
3 Chitra
4578126
103
4 Dhivya
5246642
104
5 Emy
4756895
105
6 Fredey
42563478
106
7 Haritha
4523689
101
8 Gowri
4425635
101
9 Lalitha
4563563
102
10 Kowsalya
4585695
102
10 rows selected.

DEPT_NO

SQL> select * from department;


DEPT_NO DEPT_NAME MANAGER
---------- ---------- -------------------101 CSE
AJay
102 ECE
Gowtham
103 MECH
Dharveesh
104 CIVIL Vijay
105 EEE
Kumaran
106 EI
Arun
6 rows selected.
SQL> select * from emp_manager;
EMP_NAME
MANAGER
-------------------- -------------------Abi
AJay
Banu
Gowtham
Chitra
Dharveesh
Dhivya
Vijay
Emy
Kumaran
Fredey
Arun
10

Haritha
AJay
Gowri
AJay
Lalitha
Gowtham
Kowsalya
Gowtham
10 rows selected.
SQL> select * from emp_manager where manager = 'AJay';
EMP_NAME
MANAGER
-------------------- -------------------Abi
AJay
Haritha
AJay
Gowri
AJay
SYNONYMS
SQL> create synonym emp for employee;
Synonym created.
SQL> select * from emp;
EMP_NO EMP_NAME
PH_NO
---------- -------------------- ---------- ---------1 Abi
445566
101
2 Banu
586987
102
3 Chitra
4578126
103
4 Dhivya
5246642
104
5 Emy
4756895
105
6 Fredey
42563478
106
7 Haritha
4523689
101
8 Gowri
4425635
101
9 Lalitha
4563563
102
10 Kowsalya
4585695
102

DEPT_NO

10 rows selected.
SQL> select * from emp where dept_no = '102';
EMP_NO EMP_NAME
PH_NO DEPT_NO
---------- -------------------- ---------- ---------2 Banu
586987
102
9 Lalitha
4563563
102
10 Kowsalya
4585695
102
SEQUENCES
SQL> create sequence order_no_seq
2 increment by 1
3 start with 1
4 maxvalue 100
5 minvalue 1
6 cycle
11

7 cache 10;
Sequence created.
SQL> create table sales_order(order_number number(4) primary key, order_amt number(5));
Table created.
SQL> insert into sales_order values(order_no_seq.nextval, 235);
1 row created.
SQL> select * from sales_order;
ORDER_NUMBER ORDER_AMT
------------ ---------1
235
After inserting 5 records
SQL> select * from sales_order;
ORDER_NUMBER ORDER_AMT
------------ ---------1
235
2
450
3
528
4
234
5
780
INDEXES:
SQL> create index manager on department (manager);
Index created.
SAVEPOINT
SQL> savepoint s1;
Savepoint created.
SQL> delete from employee where dept_no = '102';
3 rows deleted
SQL> select * from employee;
EMP_NO EMP_NAME
PH_NO
---------- -------------------- ---------- ---------1 Abi
445566
101
3 Chitra
4578126
103
4 Dhivya
5246642
104
5 Emy
4756895
105
6 Fredey
42563478
106
7 Haritha
4523689
101
8 Gowri
4425635
101
7 rows selected.
SQL> roll back s1;
Rollback complete.

DEPT_NO

12

SQL> select * from employee;


EMP_NO EMP_NAME
PH_NO
---------- -------------------- ---------- ---------1 Abi
445566
101
2 Banu
586987
102
3 Chitra
4578126
103
4 Dhivya
5246642
104
5 Emy
4756895
105
6 Fredey
42563478
106
7 Haritha
4523689
101
8 Gowri
4425635
101
9 Lalitha
4563563
102
10 Kowsalya
4585695
102

DEPT_NO

10 rows selected.

13

4. Creating an Employee database to set various constraints


Create table employee(emp_id number(5) primary key,emp_name varchar2(25));
SQL>desc employee;
Name
Null? Type
..
EMP_ID
NOT NULL
NUMBER(5)
EMP_NAME
VARCHAR2(25)
Valid Test Data:
SQL>insert into employee values(&emp_id,&emp_name);
SQL>select * from employee;
EMP_ID
EMP_NAME
.
10
Robert
21
Coulthard
30
Fernando Alonso
39
Kartikeyan
87
Kimmi
SQL>create table department(dept_id number(5) primary key,dept_name varchar2(20));
SQL>desc department;
Name
Null?
Type
..
DEPT_ID
NOT NULL
NUMBER(5)
DEPT_NAME
VARCHAR2(20)
SQL>insert into department values(&dept_id,&dept_name);
SQL>select * from department;
DEPT_ID
DEPT_NAME
..
100
sales
101
accounts
102
administration
103
production
104
supervisor
SQL>create table paydetails(emp_id number(5) references employee(emp_id),dept_id
number(5) references department(dept_id),basic number(7,2),deductions number(5,2),additions
number(5,2),doj date);
SQL>desc paydetails;
14

Name
Null?
Type
..
EMP_ID
NUMBER(5)
DEPT_ID
NUMBER(5)
BASIC
NUMBER(7,2)
DEDUCTIONS
NUMBER(5,2)
ADDITIONS
NUMBER(5,2)
DOJ
DATE
Different Data Sets:
SQL>insert into paydeatils values(&emp_id,&dept_id,&basic,&deductions,&additions,&doj);
SQL>select * from paydeatils;
EMP_ID DEPT_ID BASIC DEDUCTIONS
ADDITIONS DOJ

..
10
101
25023.12 43.09
71.23 08-JAN-93
21
100
10500.29 23.98
40.9
01-JAN-06
30
102
6500.5
30.54
15
06-JUL-97
39
103
9700.45
32.78
65.09 08-AUG-03
87
104
15000
97.66
154.8 24-SEP-04
SQL>create table payroll(emp_id number(5)references employee(emp_id),pay_date date);
SQL>desc payroll;
Name
Null? Type
..
EMP_ID
NUMBER(5)
PAY_DATE
DATE
SQL>insert into payroll values(&emp_id,&date);
SQL>select * from payroll;
EMP_ID
PAY_DATE
.
10
31-JAN-06
21
03-FEB-06
30
15-JAN-06
39
27-JAN-06
87
04-FEB-06
c) List the employee details department wise
SQL>select empid,deptid from paydet;
EMPID DEPTID

15

401
402
403
404
405

500
200
600
400
1200

d)

List all the employee names who joined after particular date

SQL>select e,empname from employee e,paydet p where e.empid=p.empid and p.doj>=05-mar06;


EMPNAME

AVINASH
NITIN
PHALGUN
e) List the details of employees whose basic salary is between 10,000 and 20,000
sqL> Select empid,empname from employee where salary between 10000 and 20000;
EMPID EMPNAME
.
402
AKHILA
403
aaaaaaaa
EMPID EMPNAME
.
AKHILA
f) Give a count of how many employees are working in each department
SQL>select count(empid),deptid from paydet group by deptid;
COUNT (EMPID)
DEPTID

1
200
1
400
1
500
1
600
1
1200
g) Give a names of the employees whose netsalary>10,000
SQL> select empname from employee where empid in(select empid from paydet where basicdeduction>10000);
EMPNAME

16

AVINASH
AKHILA
HARISH
NITIN
PHALGUN
h) List the details for an employee_id=5
SQL> select * from employee where empid=5;
EMPID
EMPNAME
-----------------------------------------5
Coulthard
DOMAIN INTEGRITY CONSTRAINTS
NOT NULL CONSTRAINT
SQL> create table empl (ename varchar2(30) not null, eid varchar2(20) not null);
Table created.
SQL> insert into empl values ('abcde',11);
1 row created.
SQL> insert into empl values ('fghij',12);
1 row created.
SQL> insert into empl values ('klmno',null);
insert into empl values ('klmno',null)
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("ITA"."EMPL"."EID")
SQL> select * from empl;
ENAME
EID
------------------------------ -------------------abcde
11
fghij
12
CHECK AS A COLUMN CONSTRAINT
SQL> create table depts ( dname varchar2(30) not null, did number(20) not null check
(did<10000));
Table created.
SQL> insert into depts values ('sales ',9876);
1 row created.
SQL> insert into depts values ('marketing',5432);
1 row created.
SQL> insert into depts values ('accounts',789645);
insert into depts values ('accounts',789645)
*
ERROR at line 1:
ORA-02290: check constraint (ITA.SYS_C003179) violated
17

SQL> select * from depts;


DNAME
DID
------------------------------ --------sales
9876
marketing
5432
CHECK AS A TABLE CONSTRAINT
SQL> create table airports (aname varchar2(30) not null , aid number(20) not null, acity
varchar2(30) check( acity in ('chennai','hyderabad','bangalore')));
Table created.
SQL> insert into airports values( 'abcde', 100,'chennai');
1 row created.
SQL> insert into airports values( 'fghij', 101,'hyderabad');
1 row created.
SQL> insert into airports values( 'klmno', 102,'bangalore');
1 row created.
SQL> insert into airports values( 'pqrst', 103,'mumbai');
insert into airports values( 'pqrst', 103,'mumbai')
*
ERROR at line 1:
ORA-02290: check constraint (ITA.SYS_C003187) violated
SQL> select * from airports;
ANAME
AID ACITY
------------------------------ --------- -----------------------------abcde
100
chennai
fghij
101
hyderabad
klmno
102
bangalore
ENTITY INTEGRITY CONSTRAINTS
UNIQUE AS A COLUMN CONSTRAINT
SQL> create table book (bname varchar2(30) not null, bid number(20) not null unique);
Table created.
SQL> insert into book values ('fairy tales',1000);
1 row created.
SQL> insert into book values ('bedtime stories',1001);
1 row created.
SQL> insert into book values ('comics',1001);
insert into book values ('comics',1001)
*
ERROR at line 1:
ORA-00001: unique constraint (ITA.SYS_C003130) violated
SQL> select * from book;
BNAME
BID
------------------------------ --------18

fairy tales
bedtime stories

1000
1001

UNIQUE AS A TABLE CONSTRAINT


SQL> create table orders( oname varchar2(30) not null , oid number(20) not null ,
unique(oname,oid));
Table created.
SQL> insert into orders values ('chair', 2005);
1 row created.
SQL> insert into orders values ('table',2006);
1 row created.
SQL> insert into orders values ('chair',2007);
1 row created.
SQL> insert into orders values ('chair', 2005);
insert into orders values ('chair', 2005)
*
ERROR at line 1:
ORA-00001: unique constraint (ITA.SYS_C003152) violated
SQL> select * from orders;
ONAME
OID
------------------------------ --------chair
2005
table
2006
chair
2007
PRIMARY KEY AS A COLUMN CONSTRAINT
SQL> create table custo ( cname varchar2(30) not null , cid number(20) not null primary key);
Table created.
SQL> insert into custo values ( 'jones', 506);
1 row created.
SQL> insert into custo values ('hayden',508);
1 row created.
SQL> insert into custo values ('ricky',506);
insert into custo values ('ricky',506)
*
ERROR at line 1:
ORA-00001: unique constraint (ITA.SYS_C003165) violated
SQL> select * from custo;
CNAME
CID
------------------------------ --------jones
506
hayden
508

19

PRIMARY KEY AS A TABLE CONSTRAINT


SQL> create table branches( bname varchar2(30) not null , bid number(20) not null , primary
key(bnam e,bid));
Table created.
SQL> insert into branches values ('anna nagar', 1005);
1 row created.
SQL> insert into branches values ('adyar',1006);
1 row created.
SQL> insert into branches values ('anna nagar',1007);
1 row created.
SQL> insert into branches values ('anna nagar', 1005);
insert into branches values ('anna nagar', 1005)
*
ERROR at line 1:
ORA-00001: unique constraint (ITA.SYS_C003173) violated
SQL> select * from branches;
BNAME
BID
------------------------------ --------anna nagar
1005
adyar
1006
anna nagar
1007
REFERENTIAL INTEGRITY CONSTRAINTS
TO CREATE DEPTS TABLE
SQL> create table depts(city varchar2(20), dno number(5) primary key);
Table created.
SQL> insert into depts values('chennai', 11);
1 row created.
SQL> insert into depts values('hyderabad', 22);
1 row created.
TO CREATE SEMP TABLE
SQL> create table semp(ename varchar2(20), dno number(5) references depts(dno));
Table created.
SQL> insert into semp values('x', 11);
1 row created.
SQL> insert into semp values('y', 22);
1 row created.
SQL> select * from semp;
ENAME
DNO
-------------------- --------x
11
y
22

20

ALTER TABLE
SQL> alter table semp add(eddress varchar2(20));
Table altered.
SQL> update semp set eddress='10 gandhi road' where dno=11;
1 row updated.
SQL> update semp set eddress='12 m.g. road' where dno=22;
1 row updated.
SQL> select * from semp;
ENAME
DNO EDDRESS
-------------------- --------- -------------------x
11
10 gandhi road
y
22
12 m.g. road
SQL> select city, ename from depts, s2emp where depts.dno = s2emp.dno;
CITY
ENAME
-------------------- -------------------chennai
x
hyderabad
y

21

5. Creating relationship between the databases.


Creating Dept table:
SQL> create table dept(dno number(10),dname varchar(10),loc varchar(10));
Table created.
SQL> select * from dept;
DNO DNAME LOC
---------- ---------- ---------10 inventory hyd
20 finance bglr
30 HR
mumbai
Creating emp2 table:
SQL> create table emp2(eno number(10),ename varchar(10),job varchar(10),M
er(10),dno number(10));
Table created.
SQL> select * from emp2;
ENO ENAME
JOB
MGR
DNO
---------- ---------- ---------- ---------- ---------111 saketh analyst
444
10
222 sandeep clerk
333
20
333 jagan
manager
111
10
444 madhu
engineer
222
40
1. Equijoin:
SQL> select eno,ename,job,dname,loc from emp2 e,dept d where e.dno=d.dno;
ENO ENAME
JOB
DNAME LOC
---------- ---------- ---------- ---------- ---------111 saketh analyst inventory hyd
222 sandeep clerk
finance bglr
333 jagan
manager inventory hyd
Using Clause:
SQL> select eno,ename,job,dname,loc from emp2 e join dept d using(dno);
ENO ENAME
JOB
DNAME LOC
---------- ---------- ---------- ---------- ---------111 saketh analyst inventory hyd
222 sandeep clerk
finance bglr
333 jagan
manager inventory hyd
On Clause:
SQL> select eno,ename,job,dname,loc from emp2 e join dept d on(e.dno=d.dno);
ENO ENAME
JOB
DNAME LOC
---------- ---------- ---------- ---------- ---------111 saketh analyst inventory hyd
22

222 sandeep clerk


finance bglr
333 jagan
manager inventory hyd
2. Non-Equijoin:
SQL> select eno,ename,job,dname,loc from emp2 e,dept d where e.dno>d.dno;
ENO ENAME
JOB
DNAME LOC
---------- ---------- ---------- ---------- ---------222 sandeep clerk
inventory hyd
444 madhu
engineer inventory hyd
444 madhu
engineer finance bglr
444 madhu
engineer HR
Mumbai
3. Self Join:
SQL> select e1.eno,e2.ename,e1.job,e2.dno from emp2 e1,emp2 e2 where e1.eno=e2
gr;
ENO ENAME
JOB
DNO
---------- ---------- ---------- ---------444 saketh engineer
10
333 sandeep manager
20
111 jagan
analyst
10
222 madhu
clerk
40
4. Natural Join:
SQL> select eno,ename,job,dname,loc from emp2 natural join dept;
ENO ENAME
JOB
DNAME LOC
---------- ---------- ---------- ---------- ---------111 saketh analyst inventory hyd
222 sandeep clerk
finance bglr
333 jagan
manager inventory hyd
5. Cross Join:
SQL> select eno,ename,job,dname,loc from emp2 cross join dept;
ENO ENAME
JOB
DNAME LOC
---------- ---------- ---------- ---------- ---------111 saketh analyst inventory hyd
222 sandeep clerk
inventory hyd
333 jagan
manager inventory hyd
444 madhu
engineer inventory hyd
111 saketh analyst finance bglr
222 sandeep clerk
finance bglr
333 jagan
manager finance bglr
444 madhu
engineer finance bglr
111 saketh analyst HR
mumbai
23

222 sandeep clerk


HR
333 jagan
manager HR

mumbai
mumbai

ENO ENAME
JOB
DNAME LOC
---------- ---------- ---------- ---------- ---------444 madhu
engineer HR
mumbai
12 rows selected.
6. Outer Join:
6.1 Left Outer Join:
SQL> select eno,ename,job,dname,loc from emp2 e left outer join dept d on(e.dno=
d.dno);
(OR)
SQL> select eno,ename,job,dname,loc from emp2 e,dept d where e.dno=d.dno(+);
ENO ENAME
JOB
DNAME LOC
---------- ---------- ---------- ---------- ---------333 jagan
manager inventory hyd
111 saketh analyst inventory hyd
222 sandeep clerk
finance bglr
444 madhu
engineer
6.2 Right Outer Join:
SQL> select eno,ename,job,dname,loc from emp2 e right outer join dept d on(e.dno
=d.dno);
(OR)
SQL> select eno,ename,job,dname,loc from emp2 e,dept d where e.dno(+)=d.dno;
ENO ENAME
JOB
DNAME LOC
---------- ---------- ---------- ---------- ---------111 saketh analyst inventory hyd
222 sandeep clerk
finance bglr
333 jagan
manager inventory hyd
HR
mumbai
6.3 Full Outer Join:
SQL> select eno,ename,job,dname,loc from emp2 e full outer join dept d on(e.dno=
d.dno);
ENO ENAME
JOB
DNAME LOC
---------- ---------- ---------- ---------- ---------333 jagan
manager inventory hyd
111 saketh analyst inventory hyd
222 sandeep clerk
finance bglr
444 madhu
engineer
HR
Mumbai

24

6.

Study of PL/SQL block.

1. TO DISPLAY HELLO MESSAGE


SQL> set serveroutput on;
SQL> declare
2 a varchar2(20);
3 begin
4 a:='Hello';
5 dbms_output.put_line(a);
6 end;
7 /
Hello
PL/SQL procedure successfully completed.
2. Insert the record into Sailors table by reading the values from the Keyboard.
SQL> create table sailors(sid number(10),sname varchar(10),rating number(10),age
number(10));
Table created.
SQL> set serveroutput on
SQL> declare
2 sid number(10):=&sid;
3 sname varchar(10):='&sname';
4 rating number(10):=&rating;
5 age number(10):=&age;
6 begin
7 insert into sailors values(sid,sname,rating,age);
8 end;
9 /
Enter value for sid: 02
old 2: sid number(10):=&sid;
new 2: sid number(10):=02;
Enter value for sname: lavanya
old 3: sname varchar(10):='&sname';
new 3: sname varchar(10):='lavanya';
Enter value for rating: 01
old 4: rating number(10):=&rating;
new 4: rating number(10):=01;
Enter value for age: 25
old 5: age number(10):=&age;
new 5: age number(10):=25;
PL/SQL procedure successfully completed.
SQL> /
Enter value for sid: 03
old 2: sid number(10):=&sid;
25

new 2: sid number(10):=03;


Enter value for sname: vani
old 3: sname varchar(10):='&sname';
new 3: sname varchar(10):='vani';
Enter value for rating: 02
old 4: rating number(10):=&rating;
new 4: rating number(10):=02;
Enter value for age: 25
old 5: age number(10):=&age;
new 5: age number(10):=25;
PL/SQL procedure successfully completed.
SQL> select * from sailors;
SID SNAME
RATING
---------- ---------- ---------- ---------2 lavanya
1
25
3 vani
2
25

AGE

26

7. PL/SQL block to satisfy some conditions by accepting user input


To Input A Value From The User And Display It
SQL> set serveroutput on;
SQL> declare
2 a varchar2(20);
3 begin
4 a:=&a;
5 dbms_output.put_line(a);
6 end;
7 /
Enter value for a: 5
old 4: a:=&a;
new 4: a:=5;
5
PL/SQL procedure successfully completed.
Greatest Of Three Numbers
SQL> set serveroutput on;
SQL> declare
2 a number(7);
3 b number(7);
4 c number(7);
5 begin
6 a:=&a;
7 b:=&b;
8 c:=&c;
9 if(a>b and a>c) then
10 dbms_output.put_line (' The greatest of the three is ' || a);
11 else if (b>c) then
12 dbms_output.put_line (' The greatest of the three is ' || b);
13 else
14 dbms_output.put_line (' The greatest of the three is ' || c);
15 end if;
16 end if;
17 end;
18 /
Enter value for a: 5
old 6: a:=&a;
new 6: a:=5;
Enter value for b: 7
old 7: b:=&b;
new 7: b:=7;
Enter value for c: 1
old 8: c:=&c;
new 8: c:=1;
The greatest of the three is 7
27

PL/SQL procedure successfully completed.


Print Numbers From 1 To 5 Using For Loop
SQL> set serveroutput on;
SQL> declare
2 a number:=1;
3 begin
4 for a in 1..5
5 loop
6 dbms_output.put_line (a);
7 end loop;
8 end;
9 /
1
2
3
4
5
PL/SQL procedure successfully completed.
TO CREATE SACCOUNT TABLE
SQL> create table saccount ( accno number(5), name varchar2(20), bal number(10));
Table created.
SQL> insert into saccount values ( 1,'mala',20000);
1 row created.
SQL> insert into saccount values (2,'kala',30000);
1 row created.
SQL> select * from saccount;
ACCNO NAME
BAL
--------- -------------------- --------1 mala
20000
2 kala
30000
SQL> set serveroutput on;
SQL> declare
2 a_bal number(7);
3 a_no varchar2(20);
4 debit number(7):=2000;
5 minamt number(7):=500;
6 begin
7 a_no:=&a_no;
8 select bal into a_bal from saccount where accno= a_no;
9 a_bal:= a_bal-debit;
10 if (a_bal > minamt) then
11 update saccount set bal=bal-debit where accno=a_no;
12 end if;
13 end;
14
28

15 /
Enter value for a_no: 1
old 7: a_no:=&a_no;
new 7: a_no:=1;
PL/SQL procedure successfully completed.
SQL> select * from saccount;
ACCNO NAME
BAL
--------- -------------------- --------1 mala
18000
2 kala
30000
TO CREATE TABLE SROUTES
SQL> create table sroutes ( rno number(5), origin varchar2(20), destination varchar2(20), fare
numbe
r(10), distance number(10));
Table created.
SQL> insert into sroutes values ( 2, 'chennai', 'dindugal', 400,230);
1 row created.
SQL> insert into sroutes values ( 3, 'chennai', 'madurai', 250,300);
1 row created.
SQL> insert into sroutes values ( 6, 'thanjavur', 'palani', 350,370);
1 row created.
SQL> select * from sroutes;
RNO ORIGIN
DESTINATION
FARE DISTANCE
--------- -------------------- -------------------- --------- --------2 chennai
dindugal
400
230
3 chennai
madurai
250
300
6 thanjavur
palani
350
370
SQL> set serveroutput on;
SQL> declare
2 route sroutes.rno % type;
3 fares sroutes.fare % type;
4 dist sroutes.distance % type;
5 begin
6 route:=&route;
7 select fare, distance into fares , dist from sroutes where rno=route;
8 if (dist < 250) then
9 update sroutes set fare=300 where rno=route;
10 else if dist between 250 and 370 then
11 update sroutes set fare=400 where rno=route;
12 else if (dist > 400) then
13 dbms_output.put_line('Sorry');
14 end if;
15 end if;
16 end if;
17 end;
18 /
Enter value for route: 3
29

old 6: route:=&route;
new 6: route:=3;
PL/SQL procedure successfully completed.
SQL> select * from sroutes;
RNO ORIGIN
DESTINATION
FARE DISTANCE
--------- -------------------- -------------------- --------- --------2 chennai
dindugal
400
230
3 chennai
madurai
400
300
6 thanjavur
palani
350
370

30

Ex. No: 8 PL/SQL block that handles all types of exceptions.


Program with user defined exception:
Declare
A number
B number;
C number;
Mydivide_zero EXCEPTION;
Begin
A:=&a;
B:=&b;
If(B=0) then
Raise Mydivide_zero;
else
C:=a/b;
Dbms_output.put_line(division is || C);
End if;
Exception
If (mydivide_zero) then
Dbms_output.put_line(b could not be zero);
End if;
End;

31

Valid Test Data:


Enter the value for a:
10
Enter the value for b:
0
OUTPUT
b could not be zero
Handling the Exceptions on no data found
SQL> create table employee1 (
2 id
number,
3 employee_type_id
number,
4 external_id
varchar2(30),
5 first_name
varchar2(30),
6 middle_name
varchar2(30),
7 last_name
varchar2(30),
8 name
varchar2(100),
9 birth_date
date ,
10 gender_id
number );
Table created.
SQL> create table gender (
2 id
number,
3 code
varchar2(30),
4 description
varchar2(80),
5 active_date
date
default SYSDATE not null,
6 inactive_date
date );
Table created.
SQL> insert into gender ( id, code, description ) values ( 1, 'F', 'Female' );
1 row created.
SQL> insert into gender ( id, code, description ) values ( 2, 'M', 'Male' );
1 row created.
SQL> insert into gender ( id, code, description ) values ( 3, 'U', 'Unknown' );
1 row created.
SQL> set serveroutput on size 1000000;
SQL> declare
2
3
d_birth_date
employee1.birth_date%TYPE;
4
n_gender_id
employee1.gender_id%TYPE;
5
n_selected
number := -1;
32

6
n_id
employee1.id%TYPE;
7
v_first_name
employee1.first_name%TYPE;
8
v_last_name
employee1.last_name%TYPE;
9
v_middle_name
employee1.middle_name%TYPE;
10 v_name
employee1.name%TYPE;
11
12 begin
13
v_first_name := 'JOHN';
14
v_middle_name := 'J.';
15
v_last_name := 'DOUGH';
16
v_name
:= rtrim(v_last_name||', '||v_first_name||' '||v_middle_name);
17
d_birth_date := to_date('19800101', 'YYYYMMDD');
18
19
begin
20
select id into n_gender_id from gender where code = 'M';
21
exception
22
when OTHERS then
23
raise_application_error(-20001, SQLERRM||' on select gender');
24
end;
25
26
begin
27
select id
28
into n_id
29
from employee1
30
where name
= v_name
31
and birth_date = d_birth_date
32
and gender_id = n_gender_id;
33
34
n_selected := sql%rowcount;
35
exception
36
when NO_DATA_FOUND then
37
n_selected := sql%rowcount;
38
DBMS_OUTPUT.PUT_LINE('Caught raised exception NO_DATA_FOUND');
39
when OTHERS then
40
raise_application_error(-20002, SQLERRM||' on select employee');
41
end;
42
43
DBMS_OUTPUT.PUT_LINE(to_char(n_selected)||' row(s) selected.');
44 end;
45 /
Caught raised exception NO_DATA_FOUND
0 row(s) selected.
PL/SQL procedure successfully completed.

33

9. Creation of Procedures.
Creating the table ititems and displaying the contents
SQL> create table ititems(itemid number(3), actualprice number(5), ordid number(4), prodid
number(4));
Table created.
SQL> insert into ititems values(101, 2000, 500, 201);
1 row created.
SQL> insert into ititems values(102, 3000, 1600, 202);
1 row created.
SQL> insert into ititems values(103, 4000, 600, 202);
1 row created.
SQL> select * from ititems;
ITEMID ACTUALPRICE ORDID PRODID
34

--------101
102
103

----------2000
3000
4000

-------500
1600
600

--------201
202
202

Program For General Procedure Selected Records Price Is Incremented By 500 ,


Executing The Procedure Created And Displaying The Updated Table
SQL> create procedure itsum(identity number, total number) is price number;
2 null_price exception;
3 begin
4 select actualprice into price from ititems where itemid=identity;
5 if price is null then
6 raise null_price;
7 else
8 update ititems set actualprice=actualprice+total where itemid=identity;
9 end if;
10 exception
11 when null_price then
12 dbms_output.put_line('price is null');
13 end;
14 /
Procedure created.
SQL> exec itsum(101, 500);
PL/SQL procedure successfully completed.

SQL> select * from ititems;


ITEMID ACTUALPRICE
------------------101
2500
102
3000
103
4000

ORDID
--------500
1600
600

PRODID
--------201
202
202

Procedure For In Parameter Creation, Execution


SQL> set serveroutput on;
SQL> create procedure yyy (a IN number) is price number;
2 begin
3 select actualprice into price from ititems where itemid=a;
4 dbms_output.put_line('Actual price is ' || price);
5 if price is null then
6 dbms_output.put_line('price is null');
35

7 end if;
8 end;
9 /
Procedure created.
SQL> exec yyy(103);
Actual price is 4000
PL/SQL procedure successfully completed.
Procedure For Out Parameter Creation, Execution
SQL> set serveroutput on;
SQL> create procedure zzz (a in number, b out number) is identity number;
2 begin
3 select ordid into identity from ititems where itemid=a;
4 if identity<1000 then
5 b:=100;
6 end if;
7 end;
8 /
Procedure created.
SQL> declare
2 a number;
3 b number;
4 begin
5 zzz(101,b);
6 dbms_output.put_line('The value of b is '|| b);
7 end;
8 /
The value of b is 100
PL/SQL procedure successfully completed.
Procedure For Inout Parameter Creation, Execution
SQL> create procedure itit ( a in out number) is
2 begin
3 a:=a+1;
4 end;
5 /
Procedure created.
SQL> declare
2 a number:=7;
3 begin
4 itit(a);
5 dbms_output.put_line(The updated value is ||a);
36

6 end;
7 /
The updated value is 8
PL/SQL procedure successfully completed.

37