My issue
solution
My code
DECLARE
-- Declare a cursor to retrieve the top 5 employees by salary
CURSOR top_employees_cursor IS
SELECT employee_name, salary
FROM (
SELECT employee_name, salary
FROM employees
ORDER BY salary DESC
)
WHERE ROWNUM <= 5;
-- Declare variables to hold the employee details
v_employee_name employees.employee_name%TYPE;
v_salary employees.salary%TYPE;
BEGIN
-- Open the cursor and fetch data
OPEN top_employees_cursor;
LOOP
-- Fetch the next record into variables
FETCH top_employees_cursor INTO v_employee_name, v_salary;
-- Exit the loop when no more records are found
EXIT WHEN top_employees_cursor%NOTFOUND;
-- Display the employee details
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_employee_name || ', Salary: ' || v_salary);
END LOOP;
-- Close the cursor
CLOSE top_employees_cursor;
END;
/
Learning course: Database management systems
Problem Link: Practice - Top 5 entries in Database management systems