如果我有一个名为Employee的表:
,我是Sql查询的新手Id, Name, Department
1 tim sales
2 tom sales
3 jay HR
4 ben design
5 lin design
我正在尝试编写一个返回每个部门员工数量的查询。
SELECT COUNT(Department) FROM Employee
有没有人对如何改进此查询有任何建议?
****修改
如果我想返回同一部门的员工人数
,那该怎么办?答案 0 :(得分:2)
使用分组
SELECT COUNT(id), department FROM Employee GROUP BY department
OR
按WHERE条件 SELECT COUNT(id),Department FROM Employee WHERE Department='HR'
答案 1 :(得分:1)
SELECT COUNT(id), department FROM Employee GROUP BY department;
答案 2 :(得分:0)
SELECT COUNT([id]), [department] FROM [Employee], GROUP BY [department].