Practice 4
Practice 4
Create a PL/SQL block that determines the top n salaries of the employees.
a. Create a table top_salaries with one column salary of the type NUMBER(8, 2).
b. Declare a number n that represents the number of
top n earners from the employees table.
c. Declare a variable sal of type employees.salary.
d. Declare a cursor, emp_cursor, that retrieves the salaries of employees
in descending order. Remember that the salaries should not be duplicated.
e. In the executable section, open the loop and fetch top n salaries
and insert them into top_salaries table. You can use a simple loop to operate
on the data. Also, try and use %ROWCOUNT and %FOUND attributes for the exit
condition.
f. After inserting into the top_salaries table, display the rows with a
SELECT statement. The output shown represents the five highest
salaries in the employees table.
*/
DECLARE
n NUMBER := 3;
sal hr.employees.salary%TYPE;
cursor salary_cursor is
select distinct salary
from hr.employees
order by salary desc;
BEGIN
delete from top_salaries;
open salary_cursor;
for i in 1..n loop
fetch salary_cursor into sal;
exit when salary_cursor%NOTFOUND;
insert into top_salaries (salary)
values (sal);
close salary_cursor;
END;
-------------------------------------------------------------------------------
/* 2. Create a PL/SQL block that outputs all employees
from the department 60 and with the salary more than 5000. */
DECLARE
dept_id employees.department_id%TYPE := 60;
sal employees.salary%TYPE := 5000;
BEGIN
FOR emp_table IN (SELECT * FROM employees WHERE department_id = dept_id AND
salary > sal)
LOOP
DBMS_OUTPUT.PUT_LINE(emp_table.last_name || ' earns ' || emp_table.salary);
END LOOP;
END;
-------------------------------------------------------------------------------
/* 4 Create a PL/SQL block that does the following:
a. Define a variable deptno to provide the department ID.
b. Declare a cursor, emp_cursor, that retrieves the last_name,
salary, and manager_id of the employees working
in the department specified in deptno.
c. In the executable section use the cursor
FOR loop to operate on the data retrieved.
If the salary of the employee is less than 5000
and if the manager ID is either 101 or 124,
display the message <<last_name>> Due for a raise.
Otherwise, display the message <<last_name>> Not due for a raise.
d. Test the PL/SQL block for the following cases: */
DECLARE
deptno NUMBER := 10;
l_name employees.last_name%TYPE;
sal employees.salary%TYPE;
man_id employees.manager_id%TYPE;
CURSOR emp_cursor IS
SELECT last_name, salary, manager_id
FROM employees
WHERE department_id = deptno;
BEGIN
FOR emp_table IN emp_cursor LOOP
l_name := emp_table.last_name;
sal := emp_table.salary;
man_id := emp_table.manager_id;
-------------------------------------------------------------------------------
/* Write a PL/SQL block, which declares and uses cursors with parameters.
In a loop, use a cursor to retrieve the department number and the department name
from the departments table for a department whose department_id is less than 100.
Pass the department number to another cursor as a parameter to retrieve from the
employees table the details of employee last name, job, hire date, and salary of
those employees whose employee_id is less than 120 and who work in that
department.*/
-------------------------------------------------------------------------------
DECLARE
dept_id departments.department_id%TYPE;
dept_name departments.department_name%TYPE;
emp_last_name employees.last_name%TYPE;
emp_job employees.job_id%TYPE;
emp_hire_date employees.hire_date%TYPE;
emp_salary employees.salary%TYPE;
CURSOR dept_cursor IS
SELECT department_id, department_name
FROM departments
WHERE department_id < 100
ORDER BY department_id;
END;