Given two tables, Employee and Department, generate a summary of how many employees are…
- User
Given two tables, Employee and Department, generate a summary of how many employees are in each department. Each department should be listed, whether they currently have any employees or not. The results should be sorted from high to low by number of employees, and then alphabetically by department when departments have the same number of employees. The results should list the department name followed by the employee count. The column names are not tested, so use whatever is appropriate.
- Assistant
To generate the required summary of the number of employees in each department, including departments with zero employees, you'll need to perform a
LEFT JOINbetween theDepartmentandEmployeetables. This ensures that all departments are listed, even if there are no matching employees. Then, you can useCOUNT()to count the number of employees per department and apply the necessaryGROUP BYandORDER BYclauses to meet the sorting requirements.Here's the SQL query that accomplishes this:
sqlSELECT Department.department_name, COUNT(Employee.employee_id) AS Employee_Count FROM Department LEFT JOIN Employee ON Department.department_id = Employee.department_id GROUP BY Department.department_name ORDER BY Employee_Count DESC, Department.department_name ASC;