Write a PL/SQL query to find the employees who have the highest salary in each department.

1 Answers
Answered by suresh

PL/SQL: Find Employees with Highest Salary in Each Department

PL/SQL Query for Finding Employees with Highest Salary in Each Department

To find the employees with the highest salary in each department using PL/SQL, you can use the following query:


DECLARE
    CURSOR emp_cursor IS
        SELECT department_id,
               MAX(salary) AS max_salary
        FROM employees
        GROUP BY department_id;
    
    emp_record emp_cursor%ROWTYPE;
BEGIN
    OPEN emp_cursor;
    LOOP
        FETCH emp_cursor INTO emp_record;
        EXIT WHEN emp_cursor%NOTFOUND;
        
        SELECT * FROM employees
        WHERE department_id = emp_record.department_id
        AND salary = emp_record.max_salary;
        
    END LOOP;
    CLOSE emp_cursor;
END;

Answer for Question: Write a PL/SQL query to find the employees who have the highest salary in each department.