This paste expires on 2023-06-24 19:33:51.879833. Repaste, or download this paste. . Pasted through web.

CREATE FUNCTION GetHighEarner(dept_id INT) RETURNS TABLE (Department STR, Employee STR, Salary INT)
BEGIN
  RETURN (
    SELECT d.name AS Department, e.name AS Employee, e.salary AS Salary
    FROM Employee e
    LEFT JOIN Department d ON e.departmentId = d.id
    WHERE e.departmentId = dept_id AND e.salary IN (
      SELECT DISTINCT salary
      FROM Employee
      WHERE departmentId = dept_id
      ORDER BY salary DESC
      LIMIT 3
    )
  );
END
SELECT GetHighEarner(departmentId) FROM Employee GROUP BY departmentId;
Filename: None. Size: 546b. View raw, , hex, or download this file.