PLSQL 9 2 Practice
PLSQL 9 2 Practice
com/academy
Try It / Solve It
The questions in this Practice use partial copies of the employees and departments tables. Create these copies by
executing the following SQL statements:
1. Create and execute a function sal_increase using the following two code samples. The first creates a
function which returns an employee’s new salary if a percentage increase is granted. The second calls
this function in a SELECT statement, using an increase of 5 percent.
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective
owners.
2
IS
BEGIN
RETURN (p_salary + (p_salary * p_percent_incr / 100)); END;
Now, suppose you want to see the same information in your SELECT statement, but only for those
employees for whom the increased salary would be greater than 10000. Write and test two SELECT
statements to do this. In the first, do NOT use your function. In the second, use your function. Use an
increase of 5 percent. Which do you think is better, and why?
2. Name five places within a SQL statement where a function can be used. The first one has been done for
you (think of four more).
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective
owners.
3
3. Modify your anonymous block from question 1 (the block with the calls to the sal_increase
function) to ORDER the results by the increased salary in descending order (i.e., highest increased
salary first).
4. Examine the following SELECT statement which lists the total salaries in each department for those
departments whose total salary is greater than 20000.
Modify the statement so that it also lists the total salary in each department if a 5 percent increase is
granted, and lists those departments whose increased total salary would be greater than 20000. Your
modified statement should call the sal_increase function twice, once in the column_list and once in the
HAVING clause. Test the modified statement.
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective
owners.
4
5. The following function accepts a department id as an input parameter and checks whether the
department exists in the f_depts table. Run this code to create the check_dept function.
Examine the above function and explain why it could not be used within a SQL statement. Could this
function be used within a PL/SQL statement? Why or why not?
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective
owners.
5
6. Write a procedure called insert_emp which inserts a new employee into f_emps. Pass the
employee id, last name, salary, and department id to the procedure as IN parameters. The
procedure should call your check_dept function to verify that the passed department id exists in
the f_depts table. If it exists, insert the employee. If it does not exist, use
DBMS_OUTPUT.PUT_LINE to display a suitable error message. Save your code.
7. Test your insert_emp procedure from an anonymous block using the following IN
parameter values: employee_id = 800, last_name = Jokinen, salary = 5000, and
department_id = 750. What happened and why?
8. Modify your insert_emp procedure so that if the department does not exist, the procedure first inserts a
new department with the non-existent department id and a department name of ‘Temporary’, and then
inserts the employee. Test your procedure again with the same IN values used in the previous question.
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective
owners.
6
9. Execute two SELECT statements to confirm department id 750 and employee id 800 were added to the
F_DEPTS and F_EMPS tables, respectively.
Use the get_sal function in the following SQL statement (which attempts to move all higher- salaried
employees to department 50). What happens and why?
UPDATE f_emps
SET department_id = 50
WHERE get_sal(employee_id) > 10000;
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective
owners.
7
11. Examine the following function (which doubles the salary of a chosen employee) and the SQL statement
which uses it. What will happen when the SQL statement is executed? Why? Create the upd_sal function,
then run the SELECT statement to confirm your prediction.
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective
owners.