sqloracle-databasesubqueryscalar-subquery

display the avg salary and loc of each location


Write a query to display the LOC and average salary of Each location.(Scalar Sub query). LOC is in dept table and salary is in emp table. I have to do this with scalar subquery.

select loc,(select avg(sal) from emp) 
from dept group by loc;

Solution

  • Please use below query. You have to join emp and dept table and fetch the results.

    Using join:

    select d.loc, avg(e.sal) from emp e
    inner join dept d
    on (e.dept_id = d.dept_id)   -- Changed the column it the foreign key is something else
    group by d.loc;
    

    Using sub query:

    select d.loc, (select avg(sal) from emp e where d.dept_id = e.dept_id)
    from dept d group by d.loc;
    

    enter image description here