如何从员工人数最少的部门找到员工姓名列表?
员工的表结构是:
| eid | ename | department |
答案 0 :(得分:1)
如果您希望部门员工人数最少,只需从员工表中选择行,然后按部门分组,以获取每个行的行数。您可以按升序对计数进行排序,并限制为1行以有效地获得最小值:
SELECT department, COUNT(*) AS numEmployees
FROM employees
GROUP BY department
ORDER BY numEmployees ASC
LIMIT 1;
然后,要获取该部门中所有员工的姓名,只需选择名称并将该子查询用作条件:
SELECT ename
FROM employees
WHERE department = (
SELECT department
FROM employees
GROUP BY department
ORDER BY COUNT(*) ASC
LIMIT 1);