-2

HR SCHEMA ORACLE

Create Function “GET_EMPLOYEE_SUMMARY” that receives an Employee ID and that returns, through an output parameter, a data structure with the following information: - Employee ID - First Name - Last Name - Manager Name - Department Name

So, the manager_name is baseade on employee_id..

Query:

select  

      e.first_name as first_name,
      e.last_name,
      e.employee_id,
      e.manager_id as Name,
      d.department_name

      from employees e 
      Cross join departments d 

but i have some mistakes... idk

2 Answers2

0

You just need to do a self join with the EMPLOYEE table again and join with the DEPARTMENT table should be inner join.

Something like following is needed:

SELECT
    E.FIRST_NAME   AS FIRST_NAME,
    E.LAST_NAME,
    E.EMPLOYEE_ID,
    M.FIRST_NAME || M.LAST_NAME   AS MANAGER_NAME, -- Change it according to your requirement
    D.DEPARTMENT_NAME
FROM
    EMPLOYEES E
    JOIN EMPLOYEES M ON E.MANAGER_ID = M.EMPLOYEE_ID
    JOIN DEPARTMENTS D ON D.DEPARTMENT_ID = E.DEPARTMENT_ID
WHERE
    E.EMPLOYEE_ID = 100;

Cheers!!

Popeye
  • 35,427
  • 4
  • 10
  • 31
0

Since this looks very much like homework, here is a rough guide and you'll need to fill in the blanks:

The first part of your question is to:

create Function GET_EMPLOYEE_SUMMARY

so you should not be starting by writing a SQL query but should be writing a PL/SQL statement:

CREATE FUNCTION GET_EMPLOYEE_SUMMARY ...

You can look up the complete syntax in the Oracle documentation.

that receives an Employee ID

So one of the arguments to the function should be something like:

i_employee_id IN EMPLOYEE.EMPLOYEE_ID%TYPE

and that returns, through an output parameter, a data structure with the following information ...

So your return type should be a custom type created with a statement like:

CREATE TYPE <your_type_name> AS OBJECT(
  <list_of_properties _and_data_types>
)

and then you can use that return type in your function declaration.

When it comes to write the PL/SQL block for your function then you can include your query which should look something like:

CREATE FUNCTION <your_function_signature>
IS
  <declare your local variables>
BEGIN
  SELECT E.FIRST_NAME,
         E.LAST_NAME,
         M.FIRST_NAME || ' ' || M.LAST_NAME
         D.DEPARTMENT_NAME
  INTO   <your_first_name_variable>,
         <your_last_name_variable>,
         <your_manager_name_variable>,
         <your_department_name_variable>
  FROM   EMPLOYEES E
         LEFT OUTER JOIN EMPLOYEES M
         ON ( E.MANAGER_ID = M.EMPLOYEE_ID )
         INNER JOIN DEPARTMENTS D
         ON ( D.DEPARTMENT_ID = E.DEPARTMENT_ID )
  WHERE  E.EMPLOYEE_ID = i_employee_id;

  <do stuff>

  RETURN <your_custom_type>
EXCEPTION
  WHEN NO_DATA_FOUND THEN
    <handle the exception>
END;
/
MT0
  • 143,790
  • 11
  • 59
  • 117