如何从emp表中删除第5个最高工资,他也应该来自dept表中的dallas

时间:2014-04-21 16:12:16

标签: sql

我没有想到结果。  我尝试过使用:

DELETE FROM emp c
WHERE SAL IN (
    SELECT SAL
    FROM EMP A
    WHERE DEPTNO IN (
        SELECT DEPTNO
        FROM DEPT
        WHERE LOC IN ('DALLAS')
    )
    AND 4 = (
        SELECT COUNT(*)
        FROM EMP B
        WHERE A.SAL< B.SAL
    )
);

2 个答案:

答案 0 :(得分:0)

DELETE FROM emp c
WHERE SAL IN (
    SELECT TOP 1
        sub1.SAL
    FROM (
        SELECT TOP 5
            SAL
        FROM EMP A
        inner join DEPT ON a.DEPTNO=DEPT.DEPTNO and DEPT.LOC='DALLAS'
        ORDER BY SAL DESC
    ) sub1
    ORDER BY sub1.SAL ASC
);

答案 1 :(得分:0)

薪水等级必须有助于检测第5个最高工资:

DELETE FROM emp where sal in(
   select sal from(
         SELECT sal, RANK() OVER (order by sal DESC) myrank
            FROM emp)
    where myrank=5
)
   and  DEPTNO IN (
    SELECT DEPTNO
    FROM DEPT
    WHERE LOC IN ('DALLAS')

如果您希望第5个工资在达拉斯的某个部门内,即不是所有工资,请尝试这样的事情:

DELETE FROM emp where sal in(
   select sal from(
         SELECT sal, RANK() OVER (partition by depno order by sal DESC) myrank
            FROM emp)
    where myrank=5
)
   and  DEPTNO IN (
    SELECT DEPTNO
    FROM DEPT
    WHERE LOC IN ('DALLAS')