0% found this document useful (0 votes)
13 views7 pages

Adms Lab 5

The document outlines Lab 05 for the CS236 Advanced Database Systems course, focusing on PL/SQL programming tasks. It includes instructions for modifying queries to retrieve employee details, calculate bonuses, annual salaries, manager names, and years worked based on employee IDs. Students are required to submit a PDF with SQL queries and execution snapshots from MySQL Workbench.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

Adms Lab 5

The document outlines Lab 05 for the CS236 Advanced Database Systems course, focusing on PL/SQL programming tasks. It includes instructions for modifying queries to retrieve employee details, calculate bonuses, annual salaries, manager names, and years worked based on employee IDs. Students are required to submit a PDF with SQL queries and execution snapshots from MySQL Workbench.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Department of Computer Science

CS236: Advance Database Systems

Class: BSCS-13

Lab 05: PL/SQL Start


Date: 25-02-2025
Time: 10:00-01:00 & 02:00-05:00

SAQIB MAHMOOD
460430

Instructor: Dr. Ayesha Hakim


Lab Engineer: Syed Muhammad Ali Musa

CS236: Advance Database Systems Page 1


Example
How can I modify the following query to retrieve details for a specific employee instead of
fetching all records?

You can convert your block of code into a procedure and use later like for example in above
program.

Lab Task

1) Modify the above example program such that it will display the employee first_name,
last_name and salary along with department information. Your program must get the
employee_id at run time to display the employee details.

CODE:

DECLARE
v_emp_id HR.employees.employee_id%TYPE := &Enter_Employee_ID;

CS236: Advance Database Systems Page 2


v_first_name HR.employees.first_name%TYPE;
v_last_name HR.employees.last_name%TYPE;
v_salary HR.employees.salary%TYPE;
v_department_name HR.departments.department_name%TYPE;
BEGIN
-- Retrieve Employee Details
SELECT e.first_name, e.last_name, e.salary, d.department_name
INTO v_first_name, v_last_name, v_salary, v_department_name
FROM HR.employees e
JOIN HR.departments d ON e.department_id = d.department_id
WHERE e.employee_id = v_emp_id;

-- Display Output
DBMS_OUTPUT.PUT_LINE('Employee: ' || v_first_name || ' ' || v_last_name);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);
DBMS_OUTPUT.PUT_LINE('Department: ' || v_department_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID ' || v_emp_id);
END;
/

OUTPUT:

RUNTIME EMPLOYEE ID INPUT;

CS236: Advance Database Systems Page 3


2) Display employee name along with an estimated bonus the bonus will be 20 percent of
the salary.

CODE:
DECLARE
v_emp_id HR.employees.employee_id%TYPE := &Enter_Employee_ID;
v_first_name HR.employees.first_name%TYPE;
v_last_name HR.employees.last_name%TYPE;
v_salary HR.employees.salary%TYPE;
v_bonus NUMBER;
BEGIN
SELECT first_name, last_name, salary
INTO v_first_name, v_last_name, v_salary
FROM HR.employees
WHERE employee_id = v_emp_id;

v_bonus := v_salary * 0.2;

DBMS_OUTPUT.PUT_LINE('Employee: ' || v_first_name || ' ' || v_last_name);


DBMS_OUTPUT.PUT_LINE('Bonus (20% of Salary): ' || v_bonus);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID ' || v_emp_id);
END;
/

OUTPUT:

CS236: Advance Database Systems Page 4


3) Retrieve an employee's monthly salary and calculate their annual salary. Get the
employee's id from user at run time.

CODE:

DECLARE
v_emp_id HR.employees.employee_id%TYPE := &Enter_Employee_ID;
v_salary HR.employees.salary%TYPE;
v_annual_salary NUMBER;
BEGIN
SELECT salary INTO v_salary
FROM HR.employees
WHERE employee_id = v_emp_id;

v_annual_salary := v_salary * 12;

DBMS_OUTPUT.PUT_LINE('Monthly Salary: ' || v_salary);


DBMS_OUTPUT.PUT_LINE('Annual Salary: ' || v_annual_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID ' || v_emp_id);
END;
/

OUTPUT:

4) Retrieve the manager name of the employee based on his employee_id.

CODE:

DECLARE
v_emp_id HR.employees.employee_id%TYPE := &Enter_Employee_ID;
v_manager_id HR.employees.manager_id%TYPE;
v_manager_name HR.employees.first_name%TYPE;
BEGIN
SELECT manager_id INTO v_manager_id
FROM HR.employees
WHERE employee_id = v_emp_id;

IF v_manager_id IS NOT NULL THEN


SELECT first_name INTO v_manager_name
CS236: Advance Database Systems Page 5
FROM HR.employees
WHERE employee_id = v_manager_id;

DBMS_OUTPUT.PUT_LINE('Manager Name: ' || v_manager_name);


ELSE
DBMS_OUTPUT.PUT_LINE('This employee has no manager.');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID ' || v_emp_id);
END;
/

OUTPUT:

5) Based on the hire date of an employee calculate the years they have worked. You can use
any employee id for reference.

CODE:

DECLARE
v_emp_id HR.employees.employee_id%TYPE := &Enter_Employee_ID;
v_hire_date HR.employees.hire_date%TYPE;
v_years_worked NUMBER;
BEGIN
SELECT hire_date INTO v_hire_date
FROM HR.employees
CS236: Advance Database Systems Page 6
WHERE employee_id = v_emp_id;

v_years_worked := TRUNC(MONTHS_BETWEEN(SYSDATE, v_hire_date) / 12);

DBMS_OUTPUT.PUT_LINE('Years Worked: ' || v_years_worked);


EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID ' || v_emp_id);
END;
/

OUTPUT:

Deliverables:
Submit a PDF document including the SQL queries to answer above-mentioned information
needs as well as snapshot of their outcome when executed over MySQL using the Workbench.

CS236: Advance Database Systems Page 7

You might also like