CREATE OR REPLACE FUNCTION deptsal (d NUMBER) RETURN NUMBER AS
  s NUMBER;
  p NUMBER;
BEGIN
  SELECT NVL(AVG(SAL),0) INTO s FROM EMP WHERE deptno = d;
  SELECT SAL INTO p FROM EMP WHERE empno = 7839;
  RETURN p-s;
END;
/
select dname, loc, deptsal(deptno) from dept;