DBS-BIT&BSE-Lab 10 PL SQL

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Faculty of Computing & Information Technology

CC-215-L: Database Systems Lab


BS(IT&SE) Morning - Fall 2021, Semester Spring 2023
LAB – 10
Course & Lab Instructor: Dr. Asif Sohail
Objectives:
1. PL/SQL Cursors
2. Records
3. Arrays
4. Procedures & Functions

Allowed time: 120 mins.

Instructions:
1. Gossips are not allowed.
2. Teacher assistants are for your help, so be nice with them. Respect them as they are teaching you.
Raise your hands if you have some problem and need help from TA. Avoid calling them by raising
your voice and disturbing the environment of Lab.
3. TA may deduct your marks for any kind of ill-discipline or misconduct from your side.
4. Evaluation will be considered final and you cannot debate for the marks. So, focus on performing
the tasks when the time is given to you.
5. Paste the query as well as result table screenshot as a result of each task
Sample:
Display All the Employees from emp table
Solution:
Select * from emp

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7369 SMITH CLERK 7902 12/17/1980 800 - 20

7499 ALLEN SALESMAN 7698 02/20/1981 1600 300 30

7521 WARD SALESMAN 7698 02/22/1981 1250 500 30

7566 JONES MANAGER 7839 04/02/1981 2975 - 20

7654 MARTIN SALESMAN 7698 09/28/1981 1250 1400 30


Task 01: [20 Marks]

1. Create a PL/SQL block that computes the number of records of emp table using a
cursor. (3)

2. Create a PL/SQL functions find_dname(deptno number) that receives deptno as a


parameter and returns the name of the department call this function to display the
dname of employess with their names deptno and sal. (2)

3. Create a PL/SQL function sal_status that receives salary of the employees and
returns either "above average salary" or "below average salary" on the basis of the
average salary of all the employees. (5)

4. Create a Procedure top_earners_per_department that identifies the top earners


in each department based on their salaries and displays the result.(10)

Task 02: [70 Marks]

5. Create a PL/SQL block that performs the following: (10)


a) Declare a record type named student with the fields rollno, name, cgpa.
b) Declare and array of 3 elements of the above student record type.
c) Populate the array.
d) Find the name of the student with the highest cgpa.

6. Create a PL/SQL function bonus (empno) that computes the bonus amount
(=sal*years_of_service/100+x). The value of x is determined by the job of the
employee as per the following policy: (10)

Displays the sal comm empno ename and bonus of all emplyees in emp
7. Suppose that the maximum years of service of an employee is 30 years. The salary
of the employee is raised by x% of the current salary of the year. The number x is
passed as parameter to a PL/SQL procedure. The default value of the parameter x is
10. The procedure should display the empno, ename, hiredate, retirement date,
salary at the time of hiring, and the salary at the time of retirement of the
seniormost employee. (10)

8. Create a PL/SQL procedure aggregate that performs all the built-in aggregate
functions on sal column of employee table. The procedure should display count, sum,
average, max, and min without using any one of the built-in group functions. (10)

9. Create a PL/SQL function salary_encrypt that receives salary of the employees and
encrypts the sal of an employee as per the following rules: (10)
# is equivalent of thousands
@ is equivalent of hundreds
& is equivalent of tens
The digits 0,1,2,…,9 are replaced by a,b,c,…,j respectively.
Hint: translate('ab12cd34','abcd','wxyz;) = wx12yz34;
It is assumed that the sal can be up to 4 digits, and the sal is multiple of 10.
Examples:
Code for 4530: e#f@d&
Code for 8060: i#a@g&

10. Create a PL/SQL function salary_decrypt that receives the encrypted code of the
salary that is encrypted using the rules of the last task. The function should decrypt
the code. Verify the function using dual table. (10)

11. Write a PL/SQL function dept_expense(deptno) to calculate the total expense


incurred by the company for a specified department. Consider the following: (15)

a. Include the sum of salaries (SAL) and commissions (COMM) for all employees
in the department.
b. Use the previously defined bonus function to compute the total bonus for that
department.
c. Assume that the total expense includes the total of salaries, commissions, and
bonuses for all employees in the specified department.
d. Return the calculated expense in function.
e. Displays the department wise expense of the company

12. Extend the above code to display highest expense dept and lowest one. (5)
Note: (You have to do this by expense fun you created.)

Task 03: Post Lab: [35 Marks]

1. Create a record type Product with fields product_id, product_name and stock
quantity. (4)
2. Create an array of type Product of size 10. (2)
3. Populate the array with distinct values. (4)
4. Find the product with the maximum stock and minimum stock. (5)
5. Create a function named as getUserInput which takes no parameter. It takes
input (char / varchar2 type) from user. If the input contains any non-alphabetic
character, the function will prompt the user to enter valid input and keeps on
looping. The function returns the valid input in the end. (10)
6. Use this getUserInput function in a Procedure named as “Find_Employee” which
takes input from user using the above function and find the employee/s with that
name. Use Cursors for handling more than one rows. Display all details of the
employee/s. Also use appropriate exceptions in case of “No data found”. (10)

You might also like