Oracle第二课(作业)


--部门表department
--DID NUMBER(4),    部门编号
--DNAME VARCHAR2(20),部门名词
--员工表employee
--EID NUMBER(4), 员工编号
--ENAME VARCHAR2(20), 员工姓名
--DID NUMBER(4), 部门编号
--SALARY NUMBER(40, 员工薪水


--建立部门表
CREATE TABLE JUL12_DEPARTMENT
(
DID NUMBER(4),
DNAME VARCHAR2(20)
);
INSERT INTO JUL12_DEPARTMENT VALUES (1001,'财务部');
INSERT INTO JUL12_DEPARTMENT VALUES (1002,'仓储部');
INSERT INTO JUL12_DEPARTMENT VALUES (1003,'业务部');
INSERT INTO JUL12_DEPARTMENT VALUES (1004,'客服部');
--建立员工表
CREATE TABLE JUL12_EMPLOYEE
(
    EID NUMBER(4),
    ENAME VARCHAR2(20),
    DID NUMBER(4),
    SALARY NUMBER(4)
);
INSERT INTO JUL12_EMPLOYEE VALUES (1001,'ZHANG',1001,2000);
INSERT INTO JUL12_EMPLOYEE VALUES (1002,'XIE',1001,2000);
INSERT INTO JUL12_EMPLOYEE VALUES (1003,'ASDF',1003,2000);
INSERT INTO JUL12_EMPLOYEE VALUES (1004,'EEES',1002,4000);
INSERT INTO JUL12_EMPLOYEE VALUES (1005,'SFSAF',1004,3999);
INSERT INTO JUL12_EMPLOYEE VALUES (1006,'EEE',1003,5000);
INSERT INTO JUL12_EMPLOYEE VALUES (1007,'WEDSS',1004,4000);


--15。公司有多少人和资金不为空的人数

SELECT COUNT(*)
FROM JUL12_EMPLOYEE

SELECT COUNT(SALARY)
FROM JUL12_EMPLOYEE

--16。资金大于0的人数

SELECT COUNT(*)
FROM JUL12_EMPLOYEE
WHERE SALARY >=0;

--17。各个部门平均最大,最小工资,人数,按照部门号升序排列

SELECT D.DNAME, COUNT(*) PERSONNUM,AVG(SALARY) AVGSALARY,MAX(SALARY) MAXSALARY ,MIN(SALARY) MINSALARY
FROM JUL12_DEPARTMENT D,JUL12_EMPLOYEE E
WHERE D.DID = E.DID
GROUP BY D.DNAME;

--18。工资大于1500的部门的人数

SELECT DID, COUNT(ENAME)
FROM
(
    SELECT DID ,ENAME,SALARY
    FROM JUL12_EMPLOYEE
    WHERE SALARY >1500
)
GROUP BY DID;

--19。各个部门平均工资和人数,按照部门名字升序排列

SELECT AVG(E.SALARY),COUNT(E.EID) ,D.DNAME
FROM JUL12_EMPLOYEE E,JUL12_DEPARTMENT D
GROUP BY D.DNAME
ORDER BY DNAME;

--20。是否有员工在同一部门,而且工资相同,列出这样的部门号和工资,人数

SELECT DID ,AVG(SALARY) SALARY,COUNT(EID) COUNTEID
FROM
(
    SELECT C.DID,C.SALARY,C.EID FROM JUL12_EMPLOYEE C,
    (
        SELECT A.DID, A.SALARY,A.EID
        FROM JUL12_EMPLOYEE A,JUL12_EMPLOYEE B        
        WHERE A.DID =B.DID AND A.SALARY=B.SALARY AND A.EID != B.EID
    )D
    WHERE C.DID =D.DID AND C.SALARY=D.SALARY AND C.EID != D.EID
)
GROUP BY DID

--21。哪些部门工资高于1000的人数超过2人,列出部门名字
SELECT DNAME FROM JUL12_DEPARTMENT
WHERE DID IN
(
    SELECT DID  FROM
    (
        SELECT DID FROM
        JUL12_EMPLOYEE WHERE SALARY > 1000
    )
    GROUP BY DID
    HAVING COUNT(*) >2
)

--22。哪些员工和biri同部门

SELECT * FROM JUL12_EMPLOYEE
WHERE DID IN
(
    SELECT DID FROM JUL12_EMPLOYE
    WHERE ENAME ='biri'
)

--23。哪些员工的工资,高于整个公司的平均工资,列出员工的名字和工资(降序)

SELECT ENAME ,SALARY FROM JUL12_EMPLOYEE
    WHERE SALARY > (
                            SELECT AVG(SALARY) FROM JUL12_EMPLOYEE
                        )
    ORDER BY SALARY DESC;

--24。哪些员工的工资,介于32和33部门(33高些)平均工资之间

SELECT ENAME ,SALARY
FROM JUL12_EMPLOYEE
BETWEEN
(
    SELECT AVG(SALARY) FROM JUL12_EMPLOYEE WHERE DID= 32
)
AND
(
    SELECT AVG(SALARY) FROM JUL12_EMPLOYEE WHERE DID =33
);

--25。所在部门平均工资高于1500的员工名字

SELECT ENAME FROM JUL12_EMPLOYEE
WHERE DID IN     (
                        SELECT DID FROM JUL12_EMPLOYEE
                        GROUP BY DID
                        HAVING AVG(SALARY) >1500
                    )

--26。列出各个部门的ID,以及这个部门的最高工资员工名字和工资

SELECT A.DID ,A.ENAME,A.SALARY
FROM JUL12_EMPLOYEE A,
(
    SELECT MAX(SALARY) MAXSALARY ,DID MAXDID FROM JUL12_EMPLOYEE
    GROUP BY DID
) B
WHERE A.DID=B.MAXDID AND A.SALARY = B.MAXSALARY


--27。哪个员工是整个公司的最高工资

SELECT ENAME FROM JUL12_EMPLOYEE
WHERE SALARY = (
                        SELECT MAX(SALARY)
                        FROM JUL12_EMPLOYEE
                    );

--28。部门平均工资最高的是多少
SELECT MAX (AVGSALARY)
FROM     (
            SELECT AVG(SALARY) AVGSALARY FROM JUL12_EMPLOYEE
            GROUP BY DID
        )


posted @ 2008-08-06 18:42  系咪噶  阅读(347)  评论(0编辑  收藏  举报