M
M
more112020-12-09 13:44:30
Oracle
more11, 2020-12-09 13:44:30

How to compose a join using query with an intermediate table?

Good afternoon. I can't figure out the task, please help
• Display a two-column table (HR schema). In the first column, the name and surname of the employee (first_name and last_name), separated by one space. In the second column, the address of the department for which the employee works. Specify the address of the department in the form city||''||street_address. Both fields are contained in the location table. (Because the employes and locations tables do not have common fields, use the departments table as an intermediate table). When compiling the query, use the join type …JOIN… USING

The problem is that I do not understand how to use the "intermediate table"
table employees: first_name, last_name, department_id
locations: city, street_address, location_id
departments: location_id, department_id

SELECT first_name||' '|| last_name, city||''||street_address 
FROM employees JOIN locations USING(department_id, location_id)


I understand that the error in this query is that the department_id field is not in the location table, and the location_id is not in employees
How can I add the departments staging table here using join using?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
idShura, 2020-12-09
@idShura

SELECT E.FIRST_NAME,
       E.LAST_NAME,
       L.STREET_ADDRESS
  FROM HR.EMPLOYEES E
       LEFT JOIN DEPARTMENTS D ON D.DEPARTMENT_ID = E.DEPARTMENT_ID
       LEFT JOIN LOCATIONS L ON L.LOCATION_ID = D.LOCATION_ID;
  
SELECT E.FIRST_NAME,
       E.LAST_NAME,
       L.STREET_ADDRESS
  FROM  HR.EMPLOYEES E
        LEFT JOIN DEPARTMENTS D USING (DEPARTMENT_ID)
        LEFT JOIN LOCATIONS L USING (LOCATION_ID);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question