How to write a query that returns the id of the managers in the department of which no more than 5 people work?
I have a data table employee
id integer – id employee
name char – name employee
department_id integer – id of the department in which the employee works
chief_flg boolean – flag that the employee is in a leadership position
birth_dt date – Date of Birth
salary integer – employee salary
I tried to write query like this (postgreSQL)
but I think you can do something through HAVING
SELECT COUNT(e.id)<5, e.id as id_column
FROM employee AS e
WHERE chief_flg = 'True'
GROUP BY e.id
2
Answers
Your query, no need for having:
Rather join with inner select, that counts number of employees in departments.
There are a few different ways of doing this. One method to consider is:
The basic idea is to join the chiefs (
c.chief_flag = True
) to all the employees in the same department (c.department_id = e.department_id
). As you suggested, you can then useHAVING COUNT(e.id) <= 5
to limit to the departments with 5 or fewer employees.An alternative that might be easier to follow (although is possibly less performant):
This gets all rows with
chief_flag = True
and checks whether the number of employees in their department is 5 or fewer.Another option is something like:
This is similar to the other answer on this question (calculates the number of employees in each department) but uses a window function in a subquery to do so.
Your existing attempt will not work (even if you clean up the syntax) since you are only looking at the table once and limiting to
chief_flg = 'True'
. Once you’ve limited the query to only consider chiefs, you have no way of counting the other employees. That’s why most solutions to this would either join, use a subquery, or use a window function.