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
。
答案 0 :(得分:1)
严格来说,您只需在嵌套查询中添加WHERE DEPARTMENT_ID = 50
即可,但我不确定这是否与您所获得的相同,因为通过对两者进行过滤WHERE DEPARTMENT_ID = 50
和HAVING 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)