Help JOINS ORACLE SQL query

1

Create a select where the name and last name of the employee, department where he works and the name and surname of the manager who coordinates it.

SELECT EMPLOYEES.FIRST_NAME||' '||EMPLOYEES.LAST_NAME AS "NOMBRE COMPLETO DEL EMPLEADO",DEPARTMENTS.DEPARTMENT_ID,DEPARTMENTS.DEPARTMENT_NAME,DEPARTMENTS.MANAGER_ID FROM EMPLOYEES,DEPARTMENTS WHERE EMPLOYEES.DEPARTMENT_ID=DEPARTMENTS.DEPARTMENT_ID

Good I need help for this query the problems is that I do not know how to get the name and surname of the manager who coordinates the department

    
asked by JJsCR 16.06.2018 в 08:08
source

2 answers

2

What you need in this case is to make a SELF JOIN , that is, make the union of tables with the same table of EMPLOYEES to be able to find the information of the manager.

The first point to correct is to stop using implicit% JOIN and start using explicit JOIN , since the functionality of using JOIN implicit is considered deprecated since 1992.

The query would be as follows:

SELECT 
     E.FIRST_NAME || ' ' || E.LAST_NAME AS "NOMBRE COMPLETO DEL EMPLEADO"
    ,D.DEPARTMENT_ID
    ,D.DEPARTMENT_NAME
    ,D.MANAGER_ID
    ,EE.FIRST_NAME || ' ' || EE.LAST_NAME AS "NOMBRE COMPLETO DEL MANAGER"
FROM EMPLOYEES E
    INNER JOIN DEPARTMENTS D
        ON E.DEPARMENT_ID = D.DEPARMENT_ID
    INNER JOIN EMPLOYEES EE
        ON E.MANAGER_ID = E.EMPLOYEE_ID

Magic occurs in JOIN :

INNER JOIN EMPLOYEES EE
        ON E.MANAGER_ID = E.EMPLOYEE_ID

If you can notice, by matching the field MANAGER_ID with EMPLOYEE_ID you are saying that you are going or get the manager's data through your employee id, since the manager is likewise another employee.

Note: For more information about the differences between JOIN implicit% and JOIN explicit, this great response can clear up the picture.

    
answered by 16.06.2018 / 08:38
source
1

Following the previous answer as a basis for me worked like this

SELECT E.FIRST_NAME || ' ' || E.LAST_NAME AS EMPLEADO, 
       D.DEPARTMENT_NAME AS DEPARTAMENTO, 
       EE.FIRST_NAME || ' ' || EE.LAST_NAME AS MANAGER 
       FROM EMPLOYEES E, 
            DEPARTMENTS D 
       JOIN EMPLOYEES EE 
            ON (D.MANAGER_ID=EE.EMPLOYEE_ID);
    
answered by 23.06.2018 в 02:23