Professional Documents
Culture Documents
===========================================================
declare
n int:= &n;
begin
isprime(5);
end;
/
===================================================================================
=======================================================
Soln: create or replace procedure prem_cal(health char, age int, lives char, gender
char) as
premium int;
begin
if(health='excellent' and age>=25 and age<=35 and lives='city' and
gender='Male') then
premium=:4;
dbms_output.put_line('Persons premium is ' || premium');
else
dbms_output.put_line('Person is not insured.');
end if;
end;
/
===================================================================================
=================================================
3. A company makes three products which are codified as class A, class B and class
C. On class A
items for purchase above Rs. 5000, 10% discount is given. For class B for purchase
above
Rs. 8000, a discount of 5% is given. On class C on a purchase of Rs. 10000 and
above, 4%
discount is given. Write a procedure to input product’s class and purchase amount
and display
discount along with the payable amount.
Soln:
else
dbms_output.put_line('No discount will be provided');
end if;
end;
/
exec disc('A', 6000);
===================================================================================
==================================================================
create or replace procedure disc(class char,purchaseAmount int) as
disc int;
payableAmount int;
begin
if(class = 'A' and purchaseAmount > 5000) then
disc:=10;
payableAmount:= purchaseAmount *(1 - disc/100);
dbms_output.put_line('Disc: ' || disc || '%');
dbms_output.put_line('Payable Amount: ' ||payableAmount ||
'Rs.');
elsif (class = 'B' and purchaseAmount > 8000) then
disc := 5;
payableAmount := purchaseAmount *(1 - disc/100);
dbms_output.put_line('Disc: ' || disc || '%');
dbms_output.put_line('Payable Amount: ' ||payableAmount ||
'Rs.');
elsif (class = 'C' and purchaseAmount >= 10000) then
disc := 4;
payableAmount := purchaseAmount *(1 - disc/100);
dbms_output.put_line('Disc: ' || disc || '%');
dbms_output.put_line('Payable Amount: ' ||payableAmount ||
'Rs.');
else
dbms_output.put_line('No disc');
end if;
end;
/
===================================================================================
==========================================
4. Write a procedure to input marks of three subjects. Assume that marks of each
subject is
obtained out of 50. Calculate percentage. From the following table, check
percentage range and
according to that display the grade.
Percentage Grade
>=0 and <35 F
>=35 and <50 D
>=50 and <65 C
>=65 and <80 B
>=80 A
Soln:
begin
if
(percentage>=0 and percentage<35) then
grade := 'F';
elseif
(percentage>=35 and percentage<50) then
grade := 'D';
elseif
(percentage>=50 and percentage<65) then
grade := 'D';
else
grade := 'A';
end if;
end;
/
===================================================================================
=======
create or replace procedure grade(physics int,chemistry int, maths int) as
percentage int;
grade char;
begin
if(physics <= 50 and chemistry <= 50 and maths <= 50) then
percentage:= (physics + chemistry + maths) * (100/150);
if(percentage >=0 and percentage < 35) then
grade := 'F';
elsif(percentage < 50) then
grade := 'D';
elsif(percentage < 65) then
grade := 'C';
elsif(percentage < 80) then
grade := 'B';
else
grade := 'A';
end if;
dbms_output.put_line('Grade of student is ' || grade);
else
dbms_output.put_line('Invalid Marks');
end if;
end;
/
exec grade(35,36,37);
===================================================================================
===========================================
Soln:
create or replace function salary(basic_salary int) return int as
net_salary int;
DA int;
HRA int;
MA int;
CLA int;
TA int;
PF int;
IT int;
PT int;
Allowances int;
Deductions int;
begin
DA := 0.43 * basic_salary;
HRA := 0.15 * (basic_salary + DA);
MA := 100;
CLA := 240;
TA := 800;
PF := 0.12 * (basic_salary + DA);
IT := 0.10 * basic_salary;
if (basic_salary >= 0 and basic_salary < 2000) then PT := 20;
elsif (basic_salary < 4000) then PT := 40;
elsif (basic_salary < 6000) then PT := 60;
else PT := 100;
end if;
Allowances := DA + HRA + MA + CLA + TA;
Deductions := PF + IT +PT;
net_salary := basic_salary + Allowances - Deductions;
return net_salary;
end;
/
===================================================================================
=========================================================
6. Write a function to input person’s age as total no. of days and return age of
that person in years,
months and days. (For ex., if user inputs 3456 then the age should be displayed as
9 years, 5
months and 5 days. Hint: First divide no. with 365 to get years. The quotient will
be no. of years
and reminder should be further divided by 30 to get months. When reminder is
divided with 30,
the quotient will be no. of months and reminder will be no. of days )
Soln:
create or replace function age(ageInDays int) return int as
disp varchar(255);
Years int;
months int;
days int;
temp int;
begin
Years := ageInDays / 365;
temp := REMAINDER(ageInDays, 365);
months := FLOOR(temp/30);
days := temp - (months*30);
disp := 'AGE Years: ' || Years || ' Months: ' || months || ' Days: '
|| days;
return disp;
end;
/
===================================================================================
=====================================================================
===================================================================================
=====================================================
8. Create a table PERSON with fields first_name, middle_name, last_name, age and
gender.
Person(first_name,middle_name,last_name,age,gender)
Write procedure that will retrieve records from the PERSON table and will display
person’s
name as following four formats. For Ex., if first name is Pooja, middle name is
Harshad and last
name is Shah then it should be displayed as follows.
Pooja Harshad Shah P. H. Shah Shah P.H. Shah Pooja Harshad
Soln:
create table person(
first_name varchar(50),
middle_name varchar(50),
last_name varchar(50),
age int,
gender char(1));
===================================================================================
=========================================================
9. Write a procedure that will read records from the PERSON table. Display all the
names given in
the PERSON table as per the conditions given below. For ex., if first name=Hemal,
middle
name= Harshad, last name=Shah,
a. If gender=female and age >12 then display it as Ms. Hemal Harshad Shah
b. If gender=female and age<=12 then display it as Baby Hemal Harshad Shah
c. If gender=male and age<=12 then display it as Master Hemal Harshad Shah
d. If gender=male and age>12 then display it as Mr. Hemal Harshad Shah
===================================================================================
==================================================
10. Write a procedure to display the details of STUDENT and CLASS table.
Class(classcode,classdesc)
Student(stdno,classcode,stdname,birthdate,admissiondate,gender,contact)
Soln:
create table class(
classcode char(50),
classdesc varchar(50));
if c_students%notfound then
exit;
end if;
11. Create a procedure to display employee name, salary and joining date from the
EMPLOYEE
table. Also, display total no. of years of experience of each employee from the
joining_date field
and retirement date of each employee from the birth_date field.
if c_emp%notfound then
exit;
end if;
add_months(r_emp.birth_date, 60*12)
);
end loop;
close c_emp;
end;
/
===================================================================================
=====================================================================
12. Create a procedure which will display department wise employee details in
ascending order of
department name and descending order of employee names within the department.
Department(deptno, deptname)
Employee(empno, deptno, empname, salary, birth_date, joining_date)
Soln:
create table depart(deptno int, deptname varchar(50));
create or replace procedure displayDepEmp as
cursor c_emp is select *
from employee e
join depart d
on e.deptno = d.deptno
group by d.deptno
order by d.deptname, e.empname;
r_emp c_emp%rowtype;
begin
open c_emp;
loop
fetch c_emp into r_emp;
if c_emp%notfound then
exit;
end if;
add_months(r_emp.birth_date, 60*12)
);
end loop;
close c_emp;
end;
/
===================================================================================
=================================================================
13. Write a pl/sql block to update salary of employees as per the rules given
below.
If total no. of years of experience is below 5 years, increase the salary by 5%
If total no. of years of experience is between 5 and 10 years, increase the salary
by 8%,
If total no. of years of experience is between 10 and 15, increase the salary by
10%,
In all other cases, increase the salary by 13%
Soln:
create or replace procedure updateSalary as
cursor c_emp is select * from employee;
r_emp c_emp%rowtype;
exp int:= round(months_between(sysdate,r_emp.joining_date)/12,0);
begin
open c_emp;
loop
fetch c_emp into r_emp;
if c_emp%notfound then
exit;
end if;
===================================================================================
=================================
14. Create a function which will take student name as a parameter and will return
name of the subject
in which he/she got highest marks. Use this table : Student(Id, name, DBMS_marks,
DM_marks,
DS_marks, FOP_marks, OOP_marks)
Soln:
create table Std(
Id int,
name varchar(50),
DBMS_marks int,
DM_marks int,
DS_marks int,
FOP_marks int,
OOP_marks int);
open c_std;
loop
fetch c_std into r_std;
if c_std%notfound then
exit;
end if;
end loop;
close c_std;
return maxMarks;
end;
/
===================================================================================
========================================
15. Create a procedure to display names of each student and the subject in which
he/she has obtained
highest marks along with the marks. Use this table : Student(Id, name, DBMS_marks,
DM_marks, DS_marks, FOP_marks, OOP_marks)
Soln:
create or replace procedure stdinfomark as
cursor c_np is select * from Std;
r_np c_np%rowtype;
begin
open c_np;
loop
fetch c_np into r_np;
if c_np%notfound then
exit;
end if;
end loop;
close c_np;
end;
/
***********************************************************************************
*******************************************************************