Michal David
Michal David

Reputation: 17

SQL - JOIN of two tables

I have two tables:

enter image description here

I am trying to answer this question:

Write a query that returns the cities and the number of employees associated with them. Show only cities where the average age of employees is greater than 25. Sort the table by the names of the cities in ascending order.

My answer is:

SELECT emp.Name, AVG(emp.Age), emp.id, city_emp.city
FROM Emp_det emp 
JOIN CityWorkers city_emp ON emp.id = city_emp.id
GROUP BY city_emp.city
HAVING AVG(EMP.Age) > 25 
ORDER BY city_emp.city

But this is incorrect.

Can you help me please?

Upvotes: 0

Views: 26

Answers (1)

Michal David
Michal David

Reputation: 17

SELECT COUNT(emp.id) AS num_of_emp, 
   AVG(emp.Age) AS AVG_age, 
   city_emp.city
  FROM Emp_det emp JOIN CityWorkers city_emp
  ON emp.id = city_emp.id
  GROUP BY city_emp.city
  HAVING AVG(EMP.Age) > 25 
  ORDER BY city_emp.city

Upvotes: 0

Related Questions