7/29/2019 Oracle Full
1/83
1)Write a query to view all rows and columns of a table
Select * from emp;
Output:-ENO ENAME SALARY DEPT DESG COMM BASIC_PAY
1 aman 15000 10 clerk 500 1000
2 simran 25000 20 manager 1000 1500
3 karampreet 12000 30 salesman 200 500
4 manjot 10000 30 salesman 200 500
5 gurpreet 17000 40 clerk 500 1000
6 gurnoor 8000 10 clerk 200 1000
7 hardeep 11000 30 salesman 200 500
8 manjeet 27000 50 admin - 2000
9 abhinav 15000 60 manager - 2000
2)Write a query to view selected columns and all rows
Select rno, name from student;
Output:-
RNO NAME
1 meenakshi
2 sandeep
3 sarbhjeet
4 ekjot
5 geetik
3)Write a query to view selected columns and selected rows
Select name from stu30 where rollno=1;
Output:-
NAME
meenakshi
4)Create table student with columns rollno(primary
key),name(not null),address,fathername,marks, phone no. and
perform following queries on it :-
1.Insert 5 records in the table
1
7/29/2019 Oracle Full
2/83
2.Update rollno 2s name to Sandeep
3.Update the ph_no of any student.
4.Rollno and names of student whose marks >70
5.Show details of all students
6.Show the details of rollno =57.Delete any record
create table stu30(rollno number(3) primary key,name varchar(10)
NOT NULL,address varchar(10),fathername varchar(10),marks
number(8),ph_no number(10));
desc stu30;
Output:-
Table Column Data Type Length Precision Scale
PrimaryKey Nullable Defaul t Comment
STU3 0 ROLLNO Number - 3 0 1 - - -
NAME Varchar2 10 - - - - - -
ADDRESS Varchar2 10 - - - - -
FATHERNAME Varchar2 10 - - - - -
MARKS Number - 8 0 - - -
PH_NO Number - 10 0 - - -
1.Insert 5 records in the table
insert into stu30 values(1,'meenakshi','ldh','manish',68,84736);
insert into stu30 values(2,'meenakshi','jldhr','manish',67,87364);
insert into stu30 values(3,'sarbhjeet','chd','Harpal',78,30586);
insert into stu30 values(4,'Ekjot','jal','Narinder',54,30754);
insert into stu30 values(5,'palak','chd','Gurdev',75,59264);
select * from stu30;
2
7/29/2019 Oracle Full
3/83
Output:-
ROLLNO NAME ADDRESS FATHERNAME MARKS PH_NO
1 meenakshi ldh Manish 68 84736
2 meenakshi jldhr Manish 67 87364
3 sarbhjeet chd Harpal 78 30586
4 Ekjot jal Narinder 54 30754
5 palak chd Gurdev 75 59264
2.Update rollno 2s name to Sandeep
update stu30 set name='sandeep' where rollno=2;
Output:-
ROLLNO NAME ADDRESS FATHERNAME MARKS PH_NO
1 meenakshi ldh Manish 68 84736
2 sandeep jldhr Manish 67 87364
3 sarbhjeet chd Harpal 78 30586
4 Ekjot jal Narinder 54 30754
5 palak chd Gurdev 75 59264
3.Update the ph_no of any student
update stu30 set ph_no=48294 where rollno=2;
Output:-
ROLLNO NAME ADDRESS FATHERNAME MARKS PH_NO
1 meenakshi ldh Manish 68 84736
2 sandeep jldhr Manish 67 48294
3 sarbhjeet chd Harpal 78 30586
4 Ekjot jal Narinder 54 30754
5 palak chd Gurdev 75 59264
3
7/29/2019 Oracle Full
4/83
4.Rollno and names of student whose marks are greater than
70.
select rollno, name from stu30 where marks>70;
Output:-R OL LN O N AME
3 sarbhjeet
5 palak
5.Show details of all students
select * from stu30;
Output:-
ROLLNO NAME ADDRESS FATHERNAME MARKS PH_NO
1 meenakshi ldh manish 68 84736
2 sandeep jldhr manish 67 48294
3 sarbhjeet chd Harpal 78 30586
4 Ekjot jal Narinder 54 30754
5 palak chd Gurdev 75 59264
6.Show the details of rollno =5select * from stu30 where rollno=5;
Output:-
R OL LN O N AME AD DR ESS FATH ER NAME MARKS PH _N O
5 palak chd Gurdev 75 59264
4
7/29/2019 Oracle Full
5/83
7.Delete any record
delete stu30 where rollno=5;
Output:-
ROLLNO NAME ADDRESS FATHERNAME MARKS PH_NO
1 meenakshi ldh manish 68 84736
2 sandeep jldhr manish 67 48294
3 sarbhjeet chd Harpal 78 30586
4 Ekjot jal Narinder 54 30754
5) Create table student and execute following queries:-
1.show number of students whose marks >50% and marks
7/29/2019 Oracle Full
6/83
select * from student;
Output:-
RNO NAME ENG MATH PBI
1 meenakshi 75 46 68
2 sandeep 92 67 87
3 sarbhjeet 45 36 60
4 ekjot 72 54 69
5 geetika 52 48 75
1.Show number of students whose marks >50% and marks
200;
6
7/29/2019 Oracle Full
7/83
Output:-R NO N AM E EN G+ MATH +P BI
2 sandeep 246
4.Calculate minimum and maximum marksSelect MAX(eng+math+pbi),MIN(eng+math+pbi) from student;
Output:-
MAX(ENG+MATH+PBI) MIN(ENG+MATH+PBI)
246 141
5.Show rno,marks,total marks and percentage
Select rno,eng+math+pbi as total_marks,(eng+math+pbi)/300*100 as percentage from student;
Output:-R NO TOTAL_M AR KS P ER CE NTAGE
1 189 63
2 246 82
3 141 47
4 195 65
5 175 58.33
6. Show marks of rollno 1,5 and 7.
select eng+math+pbi as marks from student where rno in(1,3,5);
Output:-MARKS
189
141
175
7
7/29/2019 Oracle Full
8/83
7.Select name, rollno,marks, if marks are less than 50 than
give 5 grace marks and then show result.
select name,rno,eng+math+pbi+5 as result from student where
eng+pbi+math
7/29/2019 Oracle Full
9/83
Output:-ENO ENAME SALARY DEPT DESG
1 aman 15000 10 clerk
2 simran 25000 20 manager
3 karampreet 12000 30 salesman
4 manjot 10000 30 salesman
5 gurpreet 17000 40 clerk
6).Create table dept with columns dno, dname, dptno , loc.
createtable dept(dno number(4), dname varchar2(20), dptno
number(4), loc varchar2(10));
Table created
insert into dept values(1,science,model town,ldh,10);
1 row(s) inserted.
insert into dept values(2,computer,dholewal,chd,20);
1 row(s) inserted.
insert into dept values(3,commerce,milar ganj,jld,30);
1 row(s) inserted.
insert into dept values(4,englisg,arti chownk,ldh,40);
1 row(s) inserted.
insert into dept values(5,punjabi,preet plc,chd,50);
1 row(s) inserted.
select * from dept;
9
7/29/2019 Oracle Full
10/83
output:-DNO DNAME LOC ADDRESS DPTNO
1 science model town ldh 10
2 computer dholewal chd 20
3 commerce milar ganj jld 30
4 englisg arti chowk ldh 40
5 punjabi preet plc chd 50
1. Put constraint foreign key on employee table column dno.
alter table emp add(constraint fk_emp foreign key(dept)
references dept(dno));
output:-
ORA-02270: no matching unique or primary key for this column-
list
2.Add constraint primary key in dept table column dno.
alter table dept add( constraint pk_dep primary key(dno));
output:-
Table altered.
3.Make column loc of dept table that their should enter thevalues only ludhiana and jalandhar.
alter table dept add(constraint ck_dep check(loc='ldh' or
loc='jalandhar'));
output:-
ORA-02293: cannot validate (SYSTEM.CK_DEP) - check
constraint violated
7).list all employees whose ename starts with A and end with
N.
select * from emp where ename like 'a%n';
10
7/29/2019 Oracle Full
11/83
Output:-ENO ENAME SALARY DEPT DESG
1 aman 15000 10 clerk
8).list all employees whose designation is clerk or managerSelect * from emp where desg= 'clerk' OR desg= 'manager';
Output:-ENO ENAME SALARY DEPT DESG
1 aman 15000 10 clerk
2 simran 25000 20 manager
5 gurpreet 17000 40 clerk
6 gurnoor 8000 10 clerk
9). List all columns whose department other than 30
select * from emp where Dept NOT IN (30);
Output:-ENO ENAME SALARY DEPT DESG
1 aman 15000 10 clerk
2 simran 25000 20 manager
5 gurpreet 17000 40 clerk
6 gurnoor 8000 10 clerk
10). List ename,designation,salary of an employee of dept no
10 and whose salary is greater than 12000.
Select ename,desg,salary from emp where dept=10 and
salary>12000;
Output:-ENAME DESG SALAR Y
aman clerk 15000
11).list all employees whose salary lies between 12500 and
30000
Select * from emp where salary BETWEEN 12500 and 30000;
11
7/29/2019 Oracle Full
12/83
Output:-
ENO ENAME SALARY DEPT DESG
1 aman 15000 10 clerk
2 simran 25000 20 manager
5 gurpreet 17000 40 clerk
12).List all employees who are not clerks
Select ename,desg from emp where desg!= 'clerk';
Output:-ENAME DESG
simran manager
karampreet salesman
manjot salesman
hardeep salesman
13).Select name of manager of department 20 whose salary is
greater than 20,000.
select ename,salary from emp where desg='manager' and dept=20and salary>20000;
Output:-E NAM E S AL AR Y
simran 25000
14).Show the details of employees who is working as salesman
and whose salary is greater than 11000.
select * from emp where desg='salesman' and salary>11000;
Output:-ENO ENAME SALARY DEPT DESG
3 karampreet 12000 30 salesman
12
7/29/2019 Oracle Full
13/83
15).Add new columns commission, basic_pay in table emp.
alter table emp add(comm number(5),basic_pay number(5));
Output:-
Table altered
update table emp set comm=500 where eno in(1,5);
2 row(s) updated
update table emp set comm=200 where eno in(4,6,7);
3 row(s) updated
update table emp set basic_pay=1000 where eno in(1,5,6);
3 row(s) updated
update table emp set basic_pay=500 where eno in(3,7);
2 row(s) updated
update table emp set basic_pay=2000 where eno in(8,9);
2 row(s) updated
update table emp set basic_pay=1500 where eno=2;1 row(s) updated
select * from emp;
Output:-ENO ENAME SALARY DEPT DESG COMM BASIC_PAY
1 aman 15000 10 clerk 500 1000
2 simran 25000 20 manager 1000 1500
3 karampreet 12000 30 salesman 200 500
4 manjot 10000 30 salesman 200 500
5 gurpreet 17000 40 clerk 500 1000
6 gurnoor 8000 10 clerk 200 1000
7 hardeep 11000 30 salesman 200 500
8 manjeet 27000 50 admin - 2000
9 abhinav 15000 60 manager - 2000
16). Show ename,basic_pay and 5% of basic_pay from table.
13
7/29/2019 Oracle Full
14/83
select ename,basic_pay,basic_pay*5/100 from emp;
Output:-ENAME BASIC_PAY BASIC_PAY*5 /100
aman 1000 50
simran 1500 75
karampreet 500 25
manjot 500 25
gurpreet 1000 50
gurnoor 1000 50
hardeep 500 25
manjeet 2000 100
abhinav 2000 100
17).Show the details of employees of department 10,20 and 30.select * from emp where dept in(10,20,30);
Output:-ENO ENAME SALARY DEPT DESG COMM BASIC_PAY
1 aman 15000 10 clerk 500 1000
2 simran 25000 20 manager 1000 1500
3 karampreet 12000 30 salesman 200 500
4 manjot 10000 30 salesman 200 500
6 gurnoor 8000 10 clerk 200 1000
7 hardeep 11000 30 salesman 200 500
18).Display ename,desg,basic_pay+500 as a bonus from table
emp.
select ename,desg,basic_pay+500 as bonus from emp;
14
7/29/2019 Oracle Full
15/83
Output:-ENAME DESG BONUS
aman clerk 1500
simran manager 2000
karampreet salesman 1000
manjot salesman 1000
gurpreet clerk 1500
gurnoor clerk 1500
hardeep salesman 1000
manjeet admin 2500
abhinav manager 2500
19).List all employees who is not getting a commission.
select ename,desg from emp where comm is NULL;
Output:-EN AME DESG
manjeet admin
abhinav manager
20).List all employees who is getting a commission.
select ename,desg from emp where comm is not NULL;
Output:-ENAME DESG
aman clerk
simran manager
karampreet salesman
manjot salesman
gurpreet clerk
gurnoor clerk
hardeep salesman
15
7/29/2019 Oracle Full
16/83
21). List all the names and enos as name is .
select eno||' name is '||ename as employees from emp;
Output:-EMPLOYEES
1 name is aman
2 name is simran
3 name iskarampreet
4 name is manjot
5 name is gurpreet
6 name is gurnoor
7 name is hardeep
8 name is manjeet
10 name is sandy
16 name is mandeep
22).Display names, total_salary of employees whose
total_salary is greater than 20000.
select ename,comm+basic_pay+salary as total_salary from emp
where comm+salary+basic_pay>20000;
Output:-ENAME TOTAL_SALARY
simran 27500
23).Show the details of students in ascending alphabetic order.
select * from emp order by ename;
Output:-ENO ENAME SALARY DEPT DESG COMM BASIC_PAY
9 abhinav 15000 60 manager - 2000
1 aman 15000 10 clerk 500 1000
6 gurnoor 8000 10 clerk 200 1000
5 gurpreet 17000 40 clerk 500 1000
7 hardeep 11000 30 salesman 200 500
3 karampreet 12000 30 salesman 200 500
8 manjeet 27000 50 admin - 2000
4 manjot 10000 30 salesman 200 500
2 simran 25000 20 manager 1000 1500
16
7/29/2019 Oracle Full
17/83
24). Names of employees whose bonus is between 10000 and
50000.
select ename,(comm+salary)-basic_pay as bonus from emp where
desg='manager' and (comm+salary)-basic_pay between 10000 and
50000;Output:-
E NAM E B ON US
simran 24500
25). Show the names of employees whose names having only 5
characters only.
select ename from emp where ename like '____';
Output:- ENAMEaman
26).Count the number of designation titles in emp table.
select COUNT(*),COUNT(desg) from emp;
Output:-COUNT(*) COUNT(DESG)
9 9
27).List the sum of sal and comm. of all employees where dept
is 20.
Select dept, SUM(salary),SUM(comm) from emp where dept=20;
Output:-
SUM(SALARY) SUM(COMM)
25000 1000
28).Calculate sum of salaries of employees department wise.
select dept,sum(salary) from emp group by dept;
Output:-
17
7/29/2019 Oracle Full
18/83
DEPT SUM(SALARY)
30 33000
20 25000
40 17000
50 27000
10 23000
60 15000
29).Count the number of employees in emp table under each
desg title.
select desg,count(desg) from emp group by desg;
Output:-D ES G C OU NT (D ES G)
salesma
n
3
clerk 3
manager 2
admin 1
30).Calculate total number of employees under each desg title
within each dept
select desg,count(desg) from emp group by dept,desg;
Output:-D ES G C OU NT( DE SG )
admin 1
manager 1
clerk 2
salesman
3
clerk 1
manager 1
31).Calculate sum of salaries group by dept using having
clause.
select dept,sum(salary) from emp group by dept having
sum(salary)
7/29/2019 Oracle Full
19/83
Output:-DEPT SUM(SALARY)
40 17000
60 15000
32).Count number of clerk in deptno 10
select count(eno) from emp where desg='clerk' and dept=10;
Output:-COUNT(ENO)
2
33).Calculate total and average salary and count number of
manager in deptno 20select count(eno),sum(salary),avg(salary) from emp where
desg='manager' and dept=20;
Output:-COUNT(ENO) SUM(SALARY) AVG(SALARY)
1 25000 25000
34).Calculate max and min sal of clerks along with
department.Select min(salary),max(salary),dept from emp where desg= 'clerk'
group by dept;
Output:-MIN(S ALARY) MAX (S ALARY) DEP T
17100 17100 40
8100 15100 10
35). Select ename, desg, dname of each employee using equi
join.
select ename,desg,dname from emp,dept where
emp.eno=dept.dno;
Output:-
19
7/29/2019 Oracle Full
20/83
ENAME DESG DNAME
aman clerk science
simran manager computer
karampreet salesman commerce
manjot salesman English
gurpreet clerk Punjabi
36).Select ename, dept and dname where desg is clerk.
select ename,emp.dept,dname from emp,dept
where emp.dept=dept.dptno and desg='clerk';
Output:-ENAME D EPT DN AME
gurnoor 10 science
aman 10 science
gurpreet 40 English
37).Select ename, desg, dptno and dname whose dptno is not 20
and 40.
select ename,desg,dept.dptno,dname from emp,dept
where emp.dept(+)=dept.dptno and dept.dptno not in(20,40);
Output:-ENAME DESG DPTNO DNAME
aman clerk 10 science
karampreet salesman 30 commerce
manjot salesman 30 commerce
gurnoor clerk 10 science
hardeep salesman 30 commerce
manjeet admin 50 punjabi
20
7/29/2019 Oracle Full
21/83
38).Select ename, dept, dname from emp and dept table using
Cartesian join.
select ename, emp.dept, dname from emp,dept order by ename;
Output:-EN AME DEPT DN AME
abhinav 60 science
abhinav 60 commerce
abhinav 60 punjabi
abhinav 60 englisg
abhinav 60 computer
aman 10 science
aman 10 commerce
aman 10 computer
aman 10 englisg
aman 10 punjabi
39).Select eno,ename from emp table where either dept=10 or
dept=30.
select eno,ename from emp where dept=10
UNION
select eno,ename from emp where dept=30;
Output:-ENO ENAME
1 aman
3 karampreet
4 manjot
6 gurnoor
7 hardeep
40).Show eno and name of employees who work for deptno 10
and deptno 40.select eno,ename from emp where dept=10
MINUS
select eno,ename from emp where dept=40;
Output:-
21
7/29/2019 Oracle Full
22/83
ENO EN AME
1 aman
6 gurnoor
41).Show the names of employees whose name start with a, j or
s using intersect operator.
select eno,ename from emp
where ename like 'a%' or ename like 's%'
intersect
select eno,ename from emp
where ename like 'a%' or ename like 'j%';
Output:-
ENO ENAME
1 aman
9 abhinav
42).List department information of all department whose
location is ldh.Select dname,dno from dept where loc = ldh;
Output:-
No data found
43).List all employees who earn more than the average salary
of all emplyees.
select ename,dept,salary from emp where salary >(select
AVG(salary) from emp e2 where emp.dept=emp.dept);
Output:-ENAME D EPT SALAR Y
simran 20 25000
gurpreet 40 17000
22
7/29/2019 Oracle Full
23/83
manjeet 50 27000
44).List all employees whose name end with N.
select eno,ename from emp where ename LIKE '%n';
Output:-ENO ENAME
1 aman
2 simran
45).List number of employees of in science department table.
select ename,salary,dname from EMP ,DEPT where
dname='science' AND EMP.dept=DEPT.dptno;
Output:-E NAM E S AL AR Y DN AM E
aman 15100 science
gurnoor 8100 science
46).List names of employees where sal>maximum sal of
deptno=20.select ename,salary from EMP where salary>(select
MAX(salary) from EMP where dept=20);
Output:-E NAM E S AL AR Y
manjeet 27000
47). List eno,ename,salary(incl. comm) and dname of allemployees.
select eno,ename,salary,salary+comm,dname from emp,dept
where emp.dept=dept.dptno;
23
7/29/2019 Oracle Full
24/83
Output:-ENO EN AME SALARY SALAR Y+COMM DN AME
1 aman 15000 15500 science
2 simran 25000 26000 computer
3 karampreet 12000 12200 commerce
4 manjot 10000 10200 commerce
5 gurpreet 17000 17500 englisg
6 gurnoor 8000 8200 science
7 hardeep 11000 11200 commerce
8 manjeet 27000 - punjabi
48).Display names of employees whose salary is greater than
maximum salary of employees working in dept=20;
select ename from emp where salary>(select max(salary) from
emp where dept=20);
Output:-ENAME
manjeet
49).Display all employees who are working in dept=10 and who
earn atleast must as any employee in dept=30.
select ename,salary,desg from emp where salary>= any(select
salary from emp where dept=30) and dept=10;
Output:-ENAME SAL ARY DESG
aman 15000 clerk
50).Display all employees who are not in dept=30 and who earnmore than all employees in dept=30.
select ename,salary,desg from emp where salary>= all(select
salary from emp where dept=30) and dept!=30;
24
7/29/2019 Oracle Full
25/83
Output:-EN AME SALAR Y DESG
aman 15000 clerk
simran 25000 manager
gurpreet 17000 clerk
manjeet 27000 admin
abhinav 1 5000 manager
51).Show name,salary,dept of the employee whose salary is
greater than employee no 5 s salary.
select eno,ename,salary,dept from emp where salary> (select
salary from emp where eno=5);
Output:-ENO ENAME SALARY DEPT
2 simran 20000 20
8 manjeet 20000 50
16 mandeep 20000 20
25
7/29/2019 Oracle Full
26/83
52)List all employees who do not work in the same
department as that of its maneger.
select * from emp where dept NOT IN (select dept from emp
where desg='manager');
Output:-
ENO E NAM E SALAR Y D EP T D ES G C OMM B AS IC _P AY
1 aman 15100 10 clerk 500 1000
3 karampreet 12100 30 salesman 200 500
4 manjot 10100 30 salesman 200 500
5 gurpreet 17100 40 clerk 500 1000
6 gurnoor 8100 10 clerk 200 1000
7 hardeep 11100 30 salesman 200 500
8 manjeet 20000 50 admin - 2000
10 sandy - 60 clerk - -
53).Create view for clerks and perform insertion
Create view clerk AS
Select eno,ename,desg,dept from emp;
Output:-View created
Insert into clerk(eno,ename,desg,dept)
Values (10,'sandy','clerk',60);
54).Create a view by enabling read only option
Create or replace view clerk AS
Select eno,ename,desg,dept from emp where dept=20 anddept=40 with read only;
55).Create a join view
Create or replace view emp_dept_veiw as
26
7/29/2019 Oracle Full
27/83
select emp.eno,emp.ename,emp.dept,dept.dname ,dept.loc
from emp,dept where dept.loc IN('ldh','jld','chd');
56). Create sequence with maximum value 50.Create SEQUENCE emp_dept
Increment by 1
Start with 1
Maxvalue 50;
Output
Sequence created
57). Insertion in table emp using sequences
Insert into emp_dept (eno,ename,salary) values (emp_dept.nextval,
preet,21000);
58).Write a query to alter sequence emp_dept
alter sequence emp_dept
increment by 2
maxvalue 100;
output
sequence altered
59)Write a query to drop sequence emp_dept
drop sequence emp_dept;
output
sequence dropped
27
7/29/2019 Oracle Full
28/83
60) Create a table STU with RNO(primary key),
NAME(should start with S or A), AGE(should not be
negative).
CREATE TABLE stu
(rno number(4) primary key,name varchar2(20) check(name like 'a%' or name like 's%'),
age number(2) check(age>0));
output:-
Table created
insert into stu values(1,'sehaj',22);
1 row(s) inserted
insert into stu values(2,'aman',21);
1 row(s) inserted
insert into stu values(3,'sandeep',22);
1 row(s) inserted
insert into stu values(4,'simran',20);
1 row(s) inserted
insert into stu values(5,'agamjeet',19);
1 row(s) inserted
select * from stu;
output:-RNO NAME AGE
1 sehaj 22
2 aman 21
3 sandeep 22
4 simran 20
5 agamjeet 19
28
7/29/2019 Oracle Full
29/83
61) Display the name and age of students whose age is same as
that of Sehaj.
select name,age from stu where age in(select age from stu where
name='sehaj');
output:-
NAME AGE
sandeep
22
sehaj 22
62) Show the details of student whose age is less than 20.select * from stu where age0),
status varchar2(10) check(status in('pass','fail','absent')));
output:-
Table created
insert into status values(2,45,'pass');1 row(s) inserted.
insert into status values(4,65,'pass');
1 row(s) inserted.
29
7/29/2019 Oracle Full
30/83
insert into status values(1,25,'fail');
1 row(s) inserted.
insert into status values(5,null,'absent');
1 row(s) inserted.
insert into status values(3,15,'fail');
1 row(s) inserted.
select * from status;
output:-R _N O M AR KS ST AT US
2 45 pass
4 65 pass
1 25 fail
5 - absent
3 15 fail
64) Show the details of absenties.
select * from status where status='absent';
output:-
65) Replace missing or Null values with not known.
select r_no,status,NVL (to_char(marks),'not known') as marks
from status;
output:-
R_N O ST AT US M AR KS2 pass 45
4 pass 65
1 fail 25
5 absent not known
3 fail 15
R _N O M AR KS ST AT US
5 - absent
30
7/29/2019 Oracle Full
31/83
PL/SQL PROGRAMS
--1Write a pl\sql program to find even number using if
statement.
declare
n number := :n;
begin
if n mod 2 =0 then
dbms_output.put_line(n || ' is even ');
end if;
end;
Output
2 is even
--2write a pl\sql program to find number is positive using if
statement.
declare
n number := :n;
begin
if n > 0 then
dbms_output.put_line(n || ' is positive ');
end if;end;
Output2 is positive
31
7/29/2019 Oracle Full
32/83
--3Write a pl\sql program to find number is positive or
negative using if-else statement.
declare
n number := :n;begin
if n > 0 then
dbms_output.put_line(n || ' is positive ');
else
dbms_output.put_line(n || ' is negative ');
end if;
end;
Output2 is positive-5 is negative
--4Write a pl\sql program to find even or odd number using if-
elsestatement.declaren number := :n;beginif n mod 2 = 0 thendbms_output.put_line(n || ' is even ');elsedbms_output.put_line(n || ' is odd');
end if;end;
Output2 is even
32
7/29/2019 Oracle Full
33/83
5 is odd
--5Write a sql program to enter the days of a week according
to numbers.
declare
n number := :n;begin
if n = 1 then
dbms_output.put_line('day is sunday');
elsif n = 2 then
dbms_output.put_line('day is monday');
elsif n = 3 then
dbms_output.put_line('day is tuesday');
elsif n = 4 thendbms_output.put_line('day is wednesday');
elsif n = 5 then
dbms_output.put_line('day is thursday');
elsif n = 6 then
dbms_output.put_line('day is friday');
elsif n = 1 then
dbms_output.put_line('day is saturday');
elsedbms_output.put_line('pls enter number b\w 1-7');
end if;
end;
Outputday is sunday
33
7/29/2019 Oracle Full
34/83
--6Write a sql program to enter the colours according to
numbers.
declare
c number := :c;
beginif c = 1 then
dbms_output.put_line('colour is green');
elsif c = 2 then
dbms_output.put_line('colour is yellow');
elsif c = 3 then
dbms_output.put_line('colour is sky');
elsif c = 4 then
dbms_output.put_line('colour is red');elsif c = 5 then
dbms_output.put_line('colour is black');
elsif c = 6 then
dbms_output.put_line('colour is blue');
elsif c = 7 then
dbms_output.put_line('colour is pink');
else
dbms_output.put_line('pls enter number b\w 1-7');end if;
end;
Outputcolour is sky
34
7/29/2019 Oracle Full
35/83
--7Write a sql program to enter the months of the year
according to numbers.
declare
m number := :m;
beginif m = 1 then
dbms_output.put_line('month is january');
elsif m = 2 then
dbms_output.put_line('month is feburary');
elsif m = 3 then
dbms_output.put_line('month is march');
elsif m = 4 then
dbms_output.put_line('month is april');elsif m = 5 then
dbms_output.put_line('month is may');
elsif m = 6 then
dbms_output.put_line('month is june');
elsif m = 7 then
dbms_output.put_line('month is july');
elsif m = 8 then
dbms_output.put_line('month is august');elsif m = 9 then
dbms_output.put_line('month is september');
elsif m = 10 then
dbms_output.put_line('month is october');
elsif m = 11 then
dbms_output.put_line('month is november');
elsif m = 12 then
dbms_output.put_line('month is december');
elsedbms_output.put_line('pls enter number b\w 1-12');
end if;
end;
Output
month is november
35
7/29/2019 Oracle Full
36/83
--8Write a pl\sql program to find factorial of number.
declare
f number := 1;
i number := 1;
n number := :n;begin
while i
7/29/2019 Oracle Full
37/83
--9Write a pl\sql program to find factorial of number and
inserting the correspondingvalues into table fact.
declare
f number := 1;i number := 1;
n number := :n;
begin
while i
7/29/2019 Oracle Full
38/83
--10 Write a pl\sql program to print counting from 1-10 using
while loop.
declare
i number := 1;begin
dbms_output.put_line('the counting is');
while i
7/29/2019 Oracle Full
39/83
--11Write pl\sql program to print table using for loop.
declare
n number := :n;begin
dbms_output.put_line(' the table of '||n|| ' is ');
for i in 1..10 loop
dbms_output.put_line( n ||' * '|| i ||' = '|| n*i );
end loop;
end;
Outputthe table of 2 is2 * 1 = 22 * 2 = 42 * 3 = 62 * 4 = 82 * 5 = 102 * 6 = 122 * 7 = 142 * 8 = 162 * 9 = 182 * 10 = 20
39
7/29/2019 Oracle Full
40/83
--12 Write a pl\sql program to print counting from 10-1 using
while loop.
declare
i number := 10;begin
dbms_output.put_line('the counting is');
while i>=1 loop
dbms_output.put_line(i);
i := i-1;
end loop;
end;
Outputthe counting is10987654321
40
7/29/2019 Oracle Full
41/83
--13Write a pl\sql program to print counting from 1-10 using
for loop.
begin
dbms_output.put_line('the counting is');for i in 1..10 loop
dbms_output.put_line( i);
end loop;
end;
Output
the counting is12345678910
41
7/29/2019 Oracle Full
42/83
--14 Write a pl\sql program to print counting from 10-1 using
for loop.
begin
dbms_output.put_line('the counting is');for i in reverse 1..10 loop
dbms_output.put_line( i);
end loop;
end;
Output
the counting is10987654321
42
7/29/2019 Oracle Full
43/83
--15To calculate simple interest.
declare
p number := :p;r number := :r;
t number := :t;
s number;
begin
s := (p*r*t)/100;
dbms_output.put_line(' the simple interest of '|| 'p = '||p||',r = '||r||',t =
'||t||' is ' ||s);
end;
Outputthe simple interest of p = 5900,r = 2.5,t =5 is 737.5
43
7/29/2019 Oracle Full
44/83
--16To calculate simple interest .
--(1) if time is greater than 2yrs and less than 4yrs then rate is
8.5
--(2) if time is greater than 4yrs and less than 6yrs then rate is5
--(3) if time is greater than 6yrs and less than 8yrs then rate is
3
--(4) else rate is 2
declare
p number := :p;
r number ;
t number := :t;s number;
begin
if(t>2 and t4 and t6 and t
7/29/2019 Oracle Full
45/83
s := (p*r*t)/100;
dbms_output.put_line(' the simple interest of '|| 'p = '||p||',r = '||
r||',t = '||t||' is ' ||s);
end if;
end;
Outputthe simple interest of p = 5900,r = 2,t = 2is 236
--17Write a pl\sql program to find sum of 10 even numbers.declares number := 0;beginfor i in 1..20 loopif i mod 2 = 0 thens := i + s;end if;
end loop;dbms_output.put_line('sum of first 10 evennumbers are ' || s);end;
Outputsum of first 10 even numbers are 110
45
7/29/2019 Oracle Full
46/83
--18Write a pl\sql program to display message "welcome to
pl\sql".begindbms_output.put_line('welcome to pl\sql');
end;
Outputwelcome to pl\sql
--19Write a pl\sql program to calculate product of two
numbers.declarea number := :a;b number := :b;p number;begin
p := a * b;dbms_output.put_line(' product of '||a ||'and '||b|| ' is '||p);end;
Outputproduct of 34 and 2 is 68
46
7/29/2019 Oracle Full
47/83
--20Write a pl\sql program to calculate division of two
numbers.declare
a number := :a;b number := :b;p number;beginp := a / b;dbms_output.put_line(' division of '||a ||'and '||b|| ' is '||p);end;
Outputdivision of 34 and 2 is 17
--21Write a pl\sql program to calculate addition of two
numbers.declarea number := :a;b number := :b;p number;beginp := a + b;dbms_output.put_line(' addition of '||a ||'
and '||b|| ' is '||p);end;
Ouputaddition of 34 and 2 is 36
47
7/29/2019 Oracle Full
48/83
--22Write a pl\sql program to calculate subtraction of two
numbers.
declare
a number := :a;
b number := :b;p number;
begin
p := a - b;
dbms_output.put_line(' difference of '||a ||' and '||b|| ' is '||p);
end;
Output
difference of 34 and 2 is 32
--23Write a pl\sql program to calculate area of circle.
declare
r number := :r;
p number := 3.14;area number;
begin
area := p*r*r;
dbms_output.put_line(' area of circle of '||r || ' is '||area);
end;
Outputarea of circle of 4 is 50.24
48
7/29/2019 Oracle Full
49/83
--24Write a pl\sql program to calculate area of rectangle.
declare
l number := :l;
b number := :b;area number;
begin
area := l*b;
dbms_output.put_line(' area of rectangle of '||l ||' and '||b|| ' is '||
area);
end;
Outputarea of rectangle of 6 and 8 is 48
--25Write a pl\sql program to calculate area of square.
declare
side number := :side;
area number;
begin
area := 4*side;
dbms_output.put_line(' area of square of '||side || ' is '||area);
end;
Outputarea of square of 4 is 16
49
7/29/2019 Oracle Full
50/83
--26Write a pl\sql program to calculate area of triangle.
declare
b number := :b;
h number := :h;area number;
begin
area := 0.5*h*b;
dbms_output.put_line(' area of triangle of '||b||' and ' ||h || ' is '||
area);
end;
Outputarea of triangle of 4.5 and 2 is 4.5
--27Write a pl\sql program to calculate telephone bill
according to the number of calls made.
declare
c number := :c;cr number := :cr;
t number ;
begin
t := c * cr;
dbms_output.put_line(' total charges for calls made are '|| t);
end;
Output
total charges for calls made are 90
50
7/29/2019 Oracle Full
51/83
--28Write a pl\sql program to find greater number among two.
declare
a number := :a;
b number := :b;begin
if a > b then
dbms_output.put_line('a is greater');
else
dbms_output.put_line('b is greater');
end if;
end;
Outputb is greater
--29Write a pl\sql program to print the following pattern.
--1--2 2
--3 3 3
begin
for i in 1..3 loop
for j in 1..i loop
dbms_output.put(i);end loop inner;
dbms_output.new_line();
end loop outer;
end;
51
7/29/2019 Oracle Full
52/83
Output122333
Table of emp1
create table emp1 (ename varchar2(20), sal number(5));
insert into emp1 values(:a, :b);
select * from emp1;
OutputENAME SAL
Aman 1250
Bhumi 1500
Sia3150
0
--30write a pl\sql program to print the ename of employee
from table emp1 if salary of the given employee is greater than
3000
declare
name emp.ename %type;
beginselect ename into name from emp1 where sal >3000;
dbms_output.put_line(' name of employee is '|| name);
end;
Outout
52
7/29/2019 Oracle Full
53/83
name of employee is sia
--31A program to display salary of an employee with specific
empno from the emp table.declare
salary emp.sal%type;
ecode emp.eno%type;
begin
ecode := :ecode;
select sal into salary from emp where eno = ecode;
dbms_output.put_line('salary is'|| salary);
end;
Outputsalary is3200
--32A program to display the use of %rowtype attribute.
declare
employee emp%rowtype;
begin
employee.eno:=115;
employee.ename :='anu';
insert into
emp(eno,ename)values(employee.eno,employee.ename);
dbms_output.put_line('row inserted');
end;
Outputrow inserted
select * from emp;
53
7/29/2019 Oracle Full
54/83
ENO ENAME SAL DEPT
101 baljeet 2000 10
100 manpreet
5500
0 10
108 amanpreet2630
010
105 kamal8950
220
106 raman7891
020
109 raju4593
030
113 geet 2500 50
114 jeet 2800 10
112 meet 1500 50
111 bhuwan 3200 40
103 muski 2000 30
121 aman 4000 50
115 anu - -
--33Write a pl/sql program to print the information of a
particular employee frim emp table using %rowtype.
declare
employee emp%rowtype;
begin
select * into employee from emp where eno=111;
dbms_output.put_line(employee.ename);dbms_output.put_line(employee.sal);
dbms_output.put_line(employee.dept);
end;
Output
54
7/29/2019 Oracle Full
55/83
bhuwan320040
55
7/29/2019 Oracle Full
56/83
--34Write a program to insert a row into table.
begin
insert into emp(eno,ename,sal,dept)values(104,'ansh',1390,70);
dbms_output.put_line('data entered');
end;
Outputdata entered
--35Write a program in pl/sql to check wheather a number is
even or odd if it is not equal to zero otherwise display themessage number is zero.
declare
n integer;
begin
n := :n;
if n0 then
if n mod 2 = 0 then
dbms_output.put_line(n||'is even');else
dbms_output.put_line(n||'is odd');
end if;
else
dbms_output.put_line('number is zero');
end if;
end;
Output5 is odd
56
7/29/2019 Oracle Full
57/83
--36Write a program in pl/sql to print the grade of students
when % of marks obtained is entered.
--per>=80 - grade A
--80>per>=60 - grade B
--60>per>=45 - grade C--45>per - fail
declare
n number;
begin
--enter percent between 1 and 100
n := :n;
if(n>=1 and n=80) then
dbms_output.put_line('grade A');
elsif(n>=60 and n=45 and n
7/29/2019 Oracle Full
58/83
--37Write a pl/sql program which executes a loop 3 times using
exit statement.
declare
n number := 0;
beginloop
n := n+1 ;
if(n>3)then
exit;
end if;
dbms_output.put_line('loop executes'||n||'times');
end loop;
end;
Outputloop executes1timesloop executes2timesloop executes3times
--38Write a pl/sql program which executes a loop 3 times using
exit when statement.
declare
n number := 0;
begin
loop
n := n+1 ;exit when n>3 ;
dbms_output.put_line('loop executes'||n||'times');
end loop;
end;
58
7/29/2019 Oracle Full
59/83
Outputloop executes1timesloop executes2timesloop executes3times
--39Write a pl/sql program to print the series of numbers
starting from the numbers n till 1.
declare
n number :=:n;
beginwhile n>=1 loop
dbms_output.put_line(n);
n :=n-1;
end loop;
end;
Output1098765432
1
59
7/29/2019 Oracle Full
60/83
--40Write a program to print the values n to 1 in the same line.
declare
n number := :n;
begin
for i in reverse 1..n loopdbms_output.put_line(i);
end loop;
dbms_output.new_line;
end;
Output6
54321
--41Write a program in pl/sql to incremant the loop index by 5.
begin
for i in 1..50 loop
if i mod 5=0 then
dbms_output.put_line(i);
end if;
end loop;
end;
60
7/29/2019 Oracle Full
61/83
Output51015
20253035404550
--42Write a program in pl/sql to show the importance of goto
statement.
begin
dbms_output.put_line('first line');
goto third;
dbms_output.put_line('second line');
dbms_output.put_line('third line');
end;
Outputfirst linethird line
Table of emp22
create table emp22(eno number(5),salnumber(5),commission number(4));insert into emp22 values(:a,:b,:c);
61
7/29/2019 Oracle Full
62/83
select * from emp22;
ENOSALCOMMISSIONENAME
10 1000
500 aman
20200
01000 mohit
30400
01500 rohit
40500
0- teena
50 6000
- meena
--43Create a user defined exceptions which checks whether the
name of the employee is blank or not during insertion.
declare
ecode emp.eno%type;
name emp.ename%type;
ename_err exception;
begin
ecode :=:ecode;
name :=:name;
if name is null then
raise ename_err;end if;
insert into emp(eno,ename) values (ecode,name);
dbms_output.put_line('data entered');
exception
when ename_err ten
62
7/29/2019 Oracle Full
63/83
dbms_output.put_line('ename should not be blank');
end;
Output
ename should not be blank--44Program to fetch the department name and location of the
given department number and if department number doesn't
exist then program will raise the user-defined exception.
declare
location dept.loc%type;
name dept.dname%type;
dno dept.dno%type;
deptno_err exception;begin
dno := :dno;
if dno not in (101,102,103,104) then
raise deptno_err;
end if ;
select dname,loc into name, location from dept where dno=dno;
dbms_output.put_line('location =' ||location);
exceptionwhen deptno_err then
dbms_output.put_line('deptno does not exist');
end;
Output
deptno does not exist
63
7/29/2019 Oracle Full
64/83
--45Program to use pragma exception_init.
declare
child_rec_present exception;
pragma exception_init(child_rec_present,-2292);begin
delete from emp where
eno=102;
dbms_output.put_line('data deleted');
exception
when child_rec_present then dbms_output.put_line('first remove
child records');
end;
Output
first remove child records
--46Program to use raise_application_error.
declarecommission emp.comm%type;
ecode emp.eno%type;
begin
ecode := :ecode;
select comm into commission from emp where eno=ecode;
if commission is null then
raise_application_error(-20991,'no commission');
else
update emp set comm=comm *1.5 where eno=ecode;end if;
exception
when no_data_found then
dbms_output.put_line('empno does not exist');
end;
64
7/29/2019 Oracle Full
65/83
Output
ORA-20991: no commission
--47Program to use sqlcode and sqlerrm in handler clauses.
declare
salary number;
ecode emp.eno%type;
begin
ecode := :ecode;
select sal into salary from emp where eno=ecode;dbms_output.put_line(salary);
exception
when OTHERS then
dbms_output.put_line('error code ='||sqlcode);
dbms_output.put_line('error message ='||sqlerrm);
end;
Outputerror code =100
error message =ORA-01403
no data found
65
7/29/2019 Oracle Full
66/83
--48Program of exception propogation.
declare
greater_sal exception;
begindeclare
salary number;
e number;
begin
e:= :e;
select sal into salary from emp where eno=e;
if salary>4000 then
raise greater_sal;end if;
dbms_output.put_line(salary);
exception
when no_data_found then
dbms_output.put_line('eno does not exist');
end;
exception
when greater_sal thendbms_output.put_line('salary for employee greater than 4000');
end;
Output
salary for employee greater than 4000
66
7/29/2019 Oracle Full
67/83
--49Program to use exceptions raised in declarations.
begin
declare
salary number(5,2) := 1500.50;begin
dbms_output.put_line(salary);
exception
when OTHERS then
dbms_output.put_line('wrong initialization');
end;
exception
when OTHERS thendbms_output.put_line('output block-wrong initialization');
end;
Output
output block-wrong initialization
67
7/29/2019 Oracle Full
68/83
--50Program to check whether record exist of the given
employee number. if so, then retrieve his salary.
declare
salary number;
beginselect sal into salary from emp where eno= :eno;
if sql%found then
dbms_output.put_line('record is found');
dbms_output.put_line('salary is '|| salary);
end if;
exception
when no_data_found then
dbms_output.put_line('employee record is not present');end;
output
record is found
salary is 410
--51Program to use sql%notfound attribute to check whether
the record is updated or not?
begin
update emp set sal=sal+100 where eno=:eno;
if sql%notfound then
dbms_output.put_line('record not found');
else
dbms_output.put_line('record is updated');
end if;end;
Output
record not found
68
7/29/2019 Oracle Full
69/83
--52Program to count the number of records affected
increasing the salaries of employees by 100 in emp table.
begin
update emp set sal= sal+100;
dbms_output.put_line(sql%rowcount || 'rows updated');end;
Output
4rows updated
--53Program to display the information of a given departmentnumber.
declare
cursor emp_data is
select eno,ename,sal from emp where dno= :dno;
ecode emp.eno%type;
name emp.ename%type;
salary emp.sal%type;
beginopen emp_data;
loop
fetch emp_data into ecode,name,salary;
exit when emp_data%notfound;
dbms_output.put_line(ecode || ' ' || name || ' ' || salary);
end loop;
close emp_data;
end;
Output
101 anu 4300
103 mani 6200
69
7/29/2019 Oracle Full
70/83
--54Program to increase the salary of the employees of the emp
table such that manager salary is increased by 10%,clerk's
salary is increased by 8% and 'salesman' salary by 5%?
declare
cursor sal_cursor isselect eno,job from emp;
post emp.job%type;
ecode emp.eno%type;
begin
open sal_cursor;
loop
fetch sal_cursor into ecode,post;
exit when sal_cursor%notfound;if post='manager' then
update emp set sal=sal*1.10 where eno=ecode;elsif post='clerk' then
update emp set sal=sal*1.08 where eno=ecode;
elsif post='salesman' then
update emp set sal=sal*1.05 where eno=ecode;
end if;
end loop;close sal_cursor;
end;
Output
Statement processed.
70
7/29/2019 Oracle Full
71/83
--55Program to determines the top 4 employees with respect
to salaries.
declare
cusor top_cursor is
select ename,sal from emp order by sal desc;slary emp.sal%type;
name emp.ename%type;
n number;
begin
open top_cursor;
if top_cursor%isopen then
loop
fetch top_cursor into name,salary;n:=top_cursor%rowcount;
exit when n>4 or top_cursor%notfound;
dbms_output.put_line(n || ' ' || name || ' ' || salary);
end loop;
end if;
close top_cursor;
end;
Output
1 anu 4640
2 mani 6510
3 geet 5170
4 gaurav 5000
71
7/29/2019 Oracle Full
72/83
--56Program to calculate maximum,minimum and average
salary department wise and display the results.
declare
cursor info_cursor isselect dno,max(sal),min(sal),avg(sal) from emp group by dno;
dcode emp.dno%type;
maxsal emp.sal%type;
minsal emp.sal%type;
avgsal emp.sal%type;
begin
open info_cursor;
loopfetch info_cursor into dcode,minsal,maxsal,avgsal;
exit when info_cursor%notfound;
dbms_output.put_line(dcode || ' ' || maxsal || ' ' || minsal || ' ' ||
avgsal);
end loop;
close info_cursor;
end;
Output
30 5000 5000 5000
20 5170 5170 5170
10 4644 6510 5577
72
7/29/2019 Oracle Full
73/83
--57Program to display the department name and location
using the single variable of % rowtype.
declare
cursor dept_cursor is
select * from dept;d_record dept % rowtype;
begin
open dept_cursor;
loop
fetch dept_cursor into d_record;
exit when dept_cursor%notfound;
dbms_output.put_line(d_record.dname || ' ' || d_record.loc);
end loop;close dept_cursor;
end;
Output
purchase jalandher
production chandigarh
73
7/29/2019 Oracle Full
74/83
--58Program to correct the gender code of all the employees.
declare
cursor g_cursor is
select eno,gender from emp;gender12 emp.gender%type;
ec emp.eno%type;
begin
open g_cursor;
loop
fetch g_cursor into ec,gender12;
exit when g_cursor%notfound;
if gender12 = 'M' thenupdate emp set gender = 'F' where eno = ec;
elsif gender12 = 'F' then
update emp set gender = 'M' where eno = ec;
end if;
end loop;
close g_cursor;
end;
Output
1 row(s) updated.
74
7/29/2019 Oracle Full
75/83
--59Program to print the department number and name of
dept table using cursor for loop?
declare
cursor dept_cursor isselect dno,dname from dept;
begin
for v in dept_cursor loop
dbms_output.put_line(v.dno || ' ' || v.dname);
end loop;
end;
Output102 purchase
103 production
--60Program to pass parameters to cursors.
declare
cursor dept_cursor ( n number) isselect eno,ename from emp where dno=n;
name emp.ename%type;
ecode emp.eno%type;
num number;
begin
num := :num;
open dept_cursor(num);
loop
fetch dept_cursor into ecode,name;exit when dept_cursor%notfound;
dbms_output.put_line(ecode || ' ' || name);
end loop;
close dept_cursor;
end;
75
7/29/2019 Oracle Full
76/83
Output
Statement processed.
--61Program to print department name and their
corressponding employee information.Ensure that department
name is not displayed with each record.
declare
cursor dep_cur is
select dno,dname from dept;
cursor emp_cur (dno number) isselect * from emp where dno=dno;
dcode dept.dno%type;
name dept.dname%type;
begin
open dep_cur;
loop
fetch dep_cur into dcode,name;
exit when dep_cur%notfound;dbms_output.put_line(name);
dbms_output.put_line('..........');
for e in emp_cur (dcode) loop
dbms_output.put_line(rpad(e.ename, 10) || ' ' || rpad(e.job, 10)
|| ' ' || e.sal);
end loop;
end loop;
close dep_cur;
end;
76
7/29/2019 Oracle Full
77/83
Output
Purchase
anu clerk 4644geet manager 5170
mani salesman 6510
gaurav supervisor 5000
Production
..........
anu clerk 4644
geet manager 5170mani salesman 6510
gaurav supervisor 5000
77
7/29/2019 Oracle Full
78/83
--62Program to list the top n salaries and the employee
namesto which they corresponds.
declare
cursor sal_cursor isselect distinct sal from emp order by sal desc ;
cursor emp_cursor(s number) is
select ename from emp where sal=s;
name emp.ename%type;
salary emp.sal%type;
n number;
begin
n := :n;open sal_cursor;
loop
fetch sal_cursor into salary;
exit when sal_cursor%rowcount>n or sal_cursor%notfound;
open emp_cursor(salary);
loop
fetch emp_cursor into name;
exit when emp_cursor%notfound;dbms_output.put_line(rpad (name, 10) || ' ' || salary);
end loop;
close emp_cursor;
end loop;
close sal_cursor;
end;
Outputmani 6510
geet 5170
gaurav 5000
anu 4644
78
7/29/2019 Oracle Full
79/83
--63Program to list the records of employee from emp table
within a given range of salaries.
declare
cursor emp_cursor (n1 number, n2 number) is
select ename,sal from emp where sal>=n1 and sal
7/29/2019 Oracle Full
80/83
Output
Trigger created
insert into emp values (105,'amrit',6700,40,3003,'salesman','M');
Output
1 row(s) inserted.
--65Create a trigger which will be fired on deleting a record
from the dept table.create or replace trigger dept_trig
before delete on dept
for each row
begin
delete from emp where dno = :old.dno;
dbms_output.put_line(sql%rowcount || 'records are deleted from
emp');
end;
Output
Trigger created.
delete from dept where dno =102;
Output
records are deleted from emp
1 row(s) deleted.
80
7/29/2019 Oracle Full
81/83
--66Create a trigger that will automatically generate a primary
key for a table.
create or replace trigger emp_primary
before insert on student
for each rowdeclare
last_val number(4);
new_val number(4);
begin
select nvl(max(rno),0) into last_val from student;
new_val := last_val+1;
:new.rno := new_val;
end;
Output
Trigger created.
insert into student values ('kriti',4,67);
Output
1 row(s) inserted.
--67Create a trigger which updates the salary of an employee
and checks whether the new salary is greater then the old
salary?
create or replace trigger sal_check
before update of sal on emp
for each row
beginif(:new.sal
7/29/2019 Oracle Full
82/83
Output
Trigger created.
update emp set sal=8000 where eno=102;
Output
1 row(s) updated.
--68Create a trigger that updates the values of the
corressponding rows in the emp table whenever the value of
dno changes in the dept table.create or replace trigger casdcade_upd
after update of dno on dept
for each row
begin
update emp
set emp.dno =:new.dno
where emp.dno =:old.dno;
end;Output
Trigger created.
update dept set dno=105 where dname='production';
Output
1 row(s) updated.
82
7/29/2019 Oracle Full
83/83
--69Create a trigger which illustrates the operation performed
by a user on emp table?
create or replace trigger opr_type
before insert or update or delete on empbegin
if inserting then
dbms_output.put_line('insert operation is performed');
elsif updating then
dbms_output.put_line('update operation is performed');
else
dbms_output.put_line('delete operation is performed');
end if;end;
Output
Trigger created.
delete from emp where eno= 102;
Output
delete operation is performed
1 row(s) deleted.