1 Answers
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;
Please login or Register to submit your answer