0

I have three table

EMPLOYEE table with ID and NAME
COMPANY table with ID and NAME
and 
SALARY table with EMPLOYEE_ID, COMPANY_ID and SALARY

I wanted to print the name of every company where the average salary is greater than or equal to 40000

My query is

Select distinct COMPANY.NAME from COMPANY,SALARY,EMPLOYEE WHERE SALARY.COMPANY_ID=COMPANY.ID and (select avg(SALARY) from SALARY,EMPLOYEE WHERE SALARY.EMPLOYEE_ID=EMPLOYEE.ID) >=40000;
1
  • 3
    We stopped writing queries this way in about 1992. Come. JOIN us. Commented Oct 7, 2017 at 22:18

3 Answers 3

11

You could group by the company name and have the condition in the having clause:

SELECT   c.name
FROM     company c
JOIN     salary s ON c.id = s.company_id
GROUP BY c.name
HAVING   AVG(salary) >= 40000
Sign up to request clarification or add additional context in comments.

Comments

2
SELECT DISTINCT COMPANY.NAME 
FROM COMPANY 
INNER JOIN SALARY ON COMPANY.ID = SALARY.COMPANY_ID 
INNER JOIN EMPLOYEE ON EMPLOYEE.ID = SALARY.EMPLOYEE_ID 
GROUP BY COMPANY.NAME HAVING AVG(SAL)>=40000

Comments

0

SELECT T1.NAME FROM COMPANY AS T1 INNER JOIN (SELECT COMPANY_ID, AVG(SALARY) AS avgSalary FROM SALARY GROUP BY COMPANY_ID) AS T2 ON T1.ID = T2.COMPANY_ID WHERE T2.avgSalary >= 40000;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.