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;
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;
On
If you really have to use scalar subquery then you can use below.
Note: if you need to use GROUP BY you have to use DISTINCT in your SELECT instead.
WITH DEPT
AS (SELECT 'TR' AS LOC, 1 DEPTID FROM DUAL
UNION ALL
SELECT 'FR' AS LOC, 2 DEPTID FROM DUAL),
EMP
AS (SELECT 15 AS SAL, 1 DEPTID FROM DUAL
UNION ALL
SELECT 20 AS SAL, 2 DEPTID FROM DUAL
UNION ALL
SELECT 35 AS SAL, 1 DEPTID FROM DUAL
UNION ALL
SELECT 45 AS SAL, 2 DEPTID FROM DUAL
)
SELECT D.LOC,
(SELECT AVG(SAL)
FROM EMP E
WHERE D.DEPTID = E.DEPTID)
FROM DEPT D
Please use below query. You have to join emp and dept table and fetch the results.
Using join:
Using sub query: