Skip to content

Instantly share code, notes, and snippets.

@sunyuding
Created August 14, 2019 05:45
Show Gist options
  • Save sunyuding/5fcdab3edfabb6a41b7f651a49aa2616 to your computer and use it in GitHub Desktop.
Save sunyuding/5fcdab3edfabb6a41b7f651a49aa2616 to your computer and use it in GitHub Desktop.
Hacker Rank MySQL Employees by department excercise
/*
Hacker Rank MySQL Employees by department excercise
Write a query to print the respective Department Name and total of employees per department for all departments in Department table
Include departments with 0 employees.
Order the results per total of employees, if 2 or more departments have the same amount of employees then order alphabetically by department name
*/
SELECT DEPARTMENT.NAME, COUNT(EMPLOYEE.ID) AS COUNT_OF_EMPLOYEES_IN_THE_DEPARTMENT
FROM DEPARMENT
LEFT JOIN EMPLOYEE ON DEPARTMENT.ID = EMPLOYEE.DEPT_ID
GROUP BY DEPARTMENT.NAME
ORDER BY COUNT_OF_EMPLOYEES_IN_THE_DEPARTMENT DESC, DEPARTMENT.NAME ASC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment