Examveda

What is the significance of the statement "GROUP BY d.name" 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

A. Aggregation of the field "name" of both table

B. Aggregation of the field "name" of table "department"

C. Sorting of the field "name"

D. None of the mentioned

Answer: Option B

Solution (By Examveda Team)

This SQL statement is designed to count the number of employees in each department. The key part you are asking about is "GROUP BY d.name".
Here's how it works:
1. `SELECT d.name, COUNT (emp_id) emp_no`: This part chooses the department name (`d.name`) and counts the number of employees (`COUNT (emp_id)`) in each department.
2. `FROM department d INNER JOIN Employee e ON d.dept_id=e.emp_id`: This joins the `department` and `Employee` tables based on the common field `dept_id`.
3. `GROUP BY d.name`: This is where the magic happens! It groups the results based on the department name (`d.name`). For each unique department name, the `COUNT(emp_id)` function counts all the employees associated with that department.
So, the correct answer is Option B:
"Aggregation of the field 'name' of table 'department'".
It means that the data is grouped by the department names, allowing you to count the employees within each specific department.

This Question Belongs to MySQL >> MySQL Miscellaneous

Join The Discussion

Related Questions on MySQL Miscellaneous