我需要编写一个查询,根据这两个表打印所有部门的相应部门名称和员工数量,并按降序排列。
Employee
---------------
ID Integer
NAME String
SALARY Integer
DEPT_ID Integer
Department
---------------
DEPT_ID Integer
NAME String
LOCATION String
答案 0 :(得分:3)
您可以按department.name
进行分组,并将count(*)
应用于每个群组:
SELECT d.name, COUNT(*)
FROM department d
JOIN employee e ON d.dept_id = e.dept_id
GROUP BY d.name
ORDER BY 2 ASC, 1 ASC