当且仅当部门号50中存在超过10名员工时,列出员工详细信息

时间:2018-04-16 21:12:39

标签: sql

CREATE TABLE EMPLOYEES 
(   
    EMPLOYEE_ID NUMERIC(6,0), 
    FIRST_NAME VARCHAR(20), 
    LAST_NAME VARCHAR(25) CONSTRAINT "EMP_LAST_NAME_NN" NOT NULL, 
    EMAIL VARCHAR(25) CONSTRAINT "EMP_EMAIL_NN" NOT NULL, 
    PHONE_NUMBER VARCHAR(20), 
    HIRE_DATE DATE CONSTRAINT "EMP_HIRE_DATE_NN" NOT NULL, 
    JOB_ID VARCHAR(10) CONSTRAINT "EMP_JOB_NN" NOT NULL, 
    SALARY NUMERIC(8,2), 
    COMMISSION_PCT NUMERIC(2,2), 
    MANAGER_ID NUMERIC(6,0), 
    DEPARTMENT_ID NUMERIC(4,0), 
);

我是SQL新手,我的任务要求我列出员工详细信息当且仅当部门编号50中有超过10名员工时。以下是我到目前为止所做的事情:

SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, HIRE_DATE
FROM EMPLOYEES
WHERE DEPARTMENT_ID IN 
    (SELECT DEPARTMENT_ID
     FROM EMPLOYEES 
     GROUP BY DEPARTMENT_ID 
     HAVING COUNT(DEPARTMENT_ID) > 10)

但我不确定如何设置条件DEPARTMENT_ID = 50

2 个答案:

答案 0 :(得分:1)

严格来说,您只需在嵌套查询中添加WHERE DEPARTMENT_ID = 50即可,但我不确定这是否与您所获得的相同,因为通过对两者进行过滤WHERE DEPARTMENT_ID = 50HAVING COUNT(DEPARTMENT_ID) > 10您基本上是这样说的:"告诉我部门50中的所有员工,但是只有超过10个员工,否则只显示我"这就是你想要的吗?

答案 1 :(得分:0)

你做了很难,添加条件很容易。您可以将其添加到子查询中:

SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, HIRE_DATE
FROM EMPLOYEES
WHERE DEPARTMENT_ID IN 
    (SELECT DEPARTMENT_ID
     FROM EMPLOYEES 
     WHERE DEPARTMENT_ID = 50
     GROUP BY DEPARTMENT_ID 
     HAVING COUNT(DEPARTMENT_ID) > 10)