0

当我们想从一个表中选择一个员工的薪水大于许多(比如说 12 个)员工的薪水时该怎么办。我知道我们必须使用子查询,但将其写为:-

Select ename,salary 
from emp 
where salary>( select salary
            from emp
           where ename='A'||ename='B'.....)

它可以这样写,但它不是一个好方法。请为此提出一些有用的查询。

4

4 回答 4

1
Select ename,salary 
from emp 
where salary > (
    select salary 
    from 
        (
        select 
           salary, 
           rownum as rn
        from emp
        order by salary
        )
    where rn = 12
)
于 2013-03-03T18:09:41.320 回答
1

如果您知道 12 名员工,我认为您想将查询编写为:

Select ename,salary 
from emp 
where salary> (select max(salary)
               from emp
               where ename in ('A', 'B', . . . )
              )

IN比一堆or语句方便多了。并且,子查询需要返回一个值,即最高工资。

于 2013-03-03T18:17:40.177 回答
0

这不是您可以使用的确切代码,但它应该对您有所帮助。

您可以使用 RANK() 函数。

oracle-base.com 上的文章示例:

SELECT empno,
       deptno,
       sal,
       RANK() OVER (PARTITION BY deptno ORDER BY sal) "rank"
FROM   emp;

     EMPNO     DEPTNO        SAL       rank
---------- ---------- ---------- ----------
      7934         10       1300          1
      7782         10       2450          2
      7839         10       5000          3
      7369         20        800          1
      7876         20       1100          2
      7566         20       2975          3
      7788         20       3000          4
      7902         20       3000          4
      7900         30        950          1
      7654         30       1250          2
      7521         30       1250          2
      7844         30       1500          4
      7499         30       1600          5
      7698         30       2850          6
于 2013-03-03T17:52:49.500 回答
0

我可以看到您的要求有两种不同的解释。

1. 哪些员工比其他(随机)12 名员工赚得更多

2. 哪些员工收入超过 12 名特定员工

这个查询解决了第一个要求,尽管它在更大的数据集上会变得非常慢。

select *
  from emp a
 where 12 = (select count(*) 
               from emp b 
              where b.salary < a.salary); 

这个查询解决了第二个需求

select * 
  from emp
 where salary > all(select salary 
                      from emp 
                     where emp_id in(1,2,3,4,5)
                   )
于 2013-03-03T18:29:50.400 回答