SQL SERVER中需要支持的选择查询

时间:2019-09-19 06:01:08

标签: sql sql-server select

我有3张这样的桌子:

- Employee_Table (Employee_Number, Employee_Name, Department_Number) 
    With Employee_Number is PRIMARY KEY, Department_Number is FOREIGN KEY;

- Employee_Skill_Table (Employee_Number, Skill_Code, Date Registered) 
    With PRIMARY KEY (Employee_Number, Skill_Code);

- Department (Department_Number, Department_Name) 
    With PRIMARY KEY (Department_Number); 

如何指定

the departments which have >= 3 employees

救救我!

2 个答案:

答案 0 :(得分:1)

如果仅需要3个以上人员的部门,请尝试此查询

SELECT d.Department_Name
FROM Department d 
JOIN Employee_Table e ON e.Department_Number = d.Department_Number
GROUP BY Department_Name
HAVING COUNT(*) >= 3

答案 1 :(得分:1)

SELECT dept.department_name 
FROM employee_table emp 
INNER JOIN department dept ON emp.department_number= dept.department_number 
GROUP BY emp.department_number 
HAVING COUNT(emp.department_number) >= 3