Examveda

What is the significance of the statement "HAVING COUNT (emp_id)>2" in the following MySQL statement?
SELECT d.name, COUNT (emp_id) emp_no
FROM department d INNER JOIN Employee e
ON d.dept_id=e.emp_id
GROUP BY d.name
HAVING COUNT (emp_id)>2

A. Filter out all rows whose total emp_id below 2

B. Selecting those rows whose total emp_id>2

C. Filter out all rows whose total emp_id below 2 & Selecting those rows whose total emp_id>2

D. None of the mentioned

Answer: Option C

Solution (By Examveda Team)

This MySQL statement is designed to count employees in each department and display the department name and the number of employees. Let's break it down:

1. `SELECT d.name, COUNT(emp_id) emp_no`: This part selects the department name (`d.name`) and counts the number of employees (`COUNT(emp_id)`) in each department. The count is given the alias `emp_no`.

2. `FROM department d INNER JOIN Employee e ON d.dept_id=e.emp_id`: This part joins the `department` table (`d`) with the `Employee` table (`e`) based on the common field `dept_id`. This connects each department to its employees.

3. `GROUP BY d.name`: This groups the results by department name, meaning we get a separate count for each department.

4. `HAVING COUNT(emp_id) > 2`: This is the key part of the query. The `HAVING` clause is used to filter the results after they've been grouped. In this case, it keeps only those department groups where the count of employees (`COUNT(emp_id)`) is greater than 2.

In simpler terms: This statement finds departments with more than 2 employees and displays the department name and the total number of employees in each of those departments.

So the answer is: Option B: Selecting those rows whose total emp_id>2

This code filters out departments with fewer than 2 employees and only displays departments with more than 2 employees.

This Question Belongs to MySQL >> MySQL Miscellaneous

Join The Discussion

Related Questions on MySQL Miscellaneous