MySQL 盘点查询

MySQL Counting Inquiry

虽然我仍在努力解决这个问题,但我希望看到您对我的代码的反馈,如果您能帮助我。我仍在努力掌握 MySQL 的概念,因此非常感谢您的帮助! 我是 运行 MySQL HR

题目要求如下:

Write SQL statement that return the number of employees in every department whose salary is more than 6000. Do not return the department where the average salary is more than 9000

下面是我的代码(不起作用)

SELECT DEPARTMENT_ID, 
       SALARY, 
       COUNT(*) 
FROM EMPLOYEES 
GROUP BY DEPARTMENT_ID
WHERE salary BETWEEN 6000 AND 9000;

我的错误消息:关键字 'WHERE' 附近的语法不正确。 https://gyazo.com/2300fbf0b0ebb43294d599cee276e235 <-- 预期输出

谢谢!

Write SQL statement that return the number of employees in every department whose salary is more than 6000. Do not return the department where the average salary is more than 9000

number of employees in every department --- count(EMPLOYEE_ID)

salary is more than 6000 --- WHERE SALARY >6000

Do not return the department where the average salary is more than 9000 ---avg(salary) <=9000

这将由以下人员完成:

SELECT DEPARTMENT_ID,
       COUNT(EMPLOYEE_ID) as nr_count
FROM   EMPLOYEES 
WHERE SALARY > 6000
GROUP BY DEPARTMENT_ID
HAVING AVG(SALARY) <=9000;

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=6165323c01f188bb0dbb8e6227bac7bc