w3resource

SQL SORTING and FILTERING on HR Database: Display those departments where any manager is managing 4 or more employees

SQL SORTING and FILTERING on HR Database: Exercise-30 with Solution

30. From the following table, write a SQL query to find those departments where a manager can manage four or more employees. Return department_id.

Sample table : employees


Sample Solution:

SELECT DISTINCT department_id
	FROM employees
		GROUP BY department_id, manager_id 
			HAVING COUNT(employee_id) >=4;

Sample Output:

 department_id
---------------
            80
            50
            60
           100
            30
(5 rows)

Relational Algebra Expression:

Relational Algebra Expression: Display those departments where any manager is managing 4 or more employees.

Relational Algebra Tree:

Relational Algebra Tree: Display those departments where any manager is managing 4 or more employees.

Practice Online


HR database model

Query Visualization:

Duration:

Query visualization of Display those departments where any manager is managing 4 or more employees - Duration

Rows:

Query visualization of Display those departments where any manager is managing 4 or more employees - Rows

Cost:

Query visualization of Display those departments where any manager is managing 4 or more employees - Cost

Contribute your code and comments through Disqus.

Previous: From the following table, write a SQL query to calculate average salary of those employees for each department who get a commission percentage. Return department id, average salary.
Next: From the following table, write a SQL query to find those departments where more than ten employees work, who got a commission percentage. Return department id.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



SQL: Tips of the Day

MySQL export schema without data

mysqldump -h yourhostnameorIP -u root -p --no-data dbname > schema.sql

Ref: https://bit.ly/3xzB9dS