SQL开发人员查询

时间:2016-04-07 05:10:24

标签: sql database oracle

问题:编写SQL SELECT语句以显示具有最大员工数的所有部门(达拉斯的部门除外)的名称和地址。按部门名称按升序对输出进行排序。

创建部门表

CREATE TABLE department
( DEPARTMENT_ID         NUMBER(4)       PRIMARY KEY,
DEPARTMENT_NAME     VARCHAR2(20)    NOT NULL UNIQUE,  
ADDRESS             VARCHAR2(20)    NOT NULL);

填充部门表

INSERT INTO department VALUES(10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO department VALUES(20, 'RESEARCH', 'DALLAS');
INSERT INTO department VALUES(30, 'SALES', 'CHICAGO');
INSERT INTO department VALUES(40, 'IT', 'DALLAS');
INSERT INTO department VALUES(50, 'EXECUTIVE', 'NEW YORK');
INSERT INTO department VALUES(60, 'MARKETING', 'CHICAGO');
COMMIT;

创建员工表

CREATE TABLE employee 
( EMPLOYEE_ID   NUMBER(4)       PRIMARY KEY,
EMPLOYEE_NAME   VARCHAR2(20)    NOT NULL,
JOB         VARCHAR2(50)    NOT NULL,
MANAGER_ID  NUMBER(4),
HIRE_DATE       DATE            NOT NULL,
SALARY      NUMBER(9, 2)    NOT NULL,
COMMISSION  NUMBER(9, 2),
DEPARTMENT_ID   NUMBER(4) REFERENCES department(DEPARTMENT_ID));

填充员工表

INSERT INTO employee 
VALUES(7839, 'KING',  'PRESIDENT', NULL, '20-NOV-01', 5000, NULL, 50);
INSERT INTO employee 
VALUES(7596, 'JOST',  'VICE PRESIDENT', 7839, '04-MAY-01', 4500, NULL, 50);
INSERT INTO employee  
VALUES(7603, 'CLARK', 'VICE PRESIDENT', 7839, '12-JUN-01', 4000, NULL, 50);
INSERT INTO employee 
VALUES(7566, 'JONES', 'PUBLIC ACCOUNTANT', 7596, '05-APR-01', 3000, NULL,         10);
INSERT INTO employee 
VALUES(7886, 'STEEL', 'PUBLIC ACCOUNTANT', 7566, '08-MAR-03', 2500, NULL, 10);
INSERT INTO employee 
VALUES(7610, 'WILSON', 'ANALYST', 7596, '03-DEC-01', 3000, NULL, 20);
INSERT INTO employee  
VALUES(7999, 'WOLFE',  'ANALYST', 7610, '15-FEB-02', 2500, NULL, 20);
INSERT INTO employee  
VALUES(7944, 'LEE', 'ANALYST', 7610, '04-SEP-06', 2400, NULL, 20);
INSERT INTO employee 
VALUES(7900, 'FISHER', 'SALESMAN', 7603, '06-DEC-01', 3000, 500, 30);
INSERT INTO employee 
VALUES(7921, 'JACKSON', 'SALESMAN', 7900, '25-FEB-05', 2500, 400, 30);
INSERT INTO employee 
VALUES(7952, 'LANCASTER', 'SALESMAN',  7900, '06-DEC-06', 2000, 150, 30);
INSERT INTO employee  
VALUES(7910, 'SMITH', 'DATABASE ADMINISTRATOR', 7596, '20-DEC-01', 2900,     NULL, 40);
INSERT INTO employee  
VALUES(7788, 'SCOTT', 'PROGRAMMER', 7910, '15-JAN-03', 2500, NULL, 40);
INSERT INTO employee 
VALUES(7876, 'ADAMS', 'PROGRAMMER', 7910, '15-JAN-03', 2000, NULL, 40);
INSERT INTO employee 
VALUES(7934, 'MILLER','PROGRAMMER', 7876, '25-JAN-02', 1000, NULL, 40);
INSERT INTO employee 
VALUES(8000, 'BREWSTER',  'TBA',  NULL,   '22-AUG-13', 2500, NULL, NULL);
COMMIT;

我写的查询在下面,它完美地找到了DALLAS部门的最大员工数,但是我想要删除DALLAS并找到其中有最大数量的员工在其中工作的其他部门,这是由数据是(芝加哥和纽约)。有人可以帮帮我吗?

SELECT  d.department_id, d.department_name, COUNT(*)
FROM        department d
INNER JOIN  employee e
ON      d.department_id = e.department_id
GROUP BY    d.department_id, d.department_name
HAVING  COUNT(*) = (SELECT  MAX(COUNT(*))
FROM        employee
GROUP BY    department_id) 
ORDER BY    department_id;

2 个答案:

答案 0 :(得分:1)

使用您的查询:

SELECT  d.department_id, d.department_name, COUNT(*)
FROM        department d
INNER JOIN  employee e
ON      d.deptartment_id = e.department_id
where d.address != 'DALLAS'
GROUP BY    d.department_id, d.department_name
HAVING  COUNT(*) = (SELECT  MAX(COUNT(*))
FROM        employee
where department_id not in (select department_id from department where address = 'DALLAS')
GROUP BY    department_id) 
ORDER BY    department_id;

答案 1 :(得分:0)

您可以使用排名分析功能:

select *
from (
    select department_id, department_name, rank() over (order by cnt desc) rnk
    from(
        SELECT  d.department_id, d.department_name, COUNT(*) cnt
        FROM        department d
        INNER JOIN  employee e
        ON      d.department_id = e.department_id
        GROUP BY    d.department_id, d.department_name
    )
)
where rnk = 2 --here you may change to what position you want
;