oracle 下面的SQL查询中的(2)是什么意思

s71maibg  于 2023-05-28  发布在  Oracle
关注(0)|答案(2)|浏览(264)

请解释一下为什么在下面的查询中使用2

select * from employee e 
where(2) = (select count(distinct(e1.sal))
            from employee e1
            where e.sal > e1.sal);
wz8daaqr

wz8daaqr1#

查询返回Salary高于2其他薪资的所有员工。括号是误导性的,不需要。

select * from employee e 
where 2 = (select count(distinct(e1.sal))
            from employee e1
            where e.sal > e1.sal);

例如,给定以下员工数据:

Employee  Salary  Count     Greater than
                       (Distinct) these Salaries
       Joe     $80,000     0      Lowest Salary
       Kate    $80,000     0      Lowest Salary
       Lee     $85,000     1      $80,000
       Chris   $85,000     1      $80,000
       Matt    $85,000     1      $80,000
       Mike    $90,000     2      $85,000,$80,000
       June    $90,000     2      $85,000,$80,000     
       Jack    $100,000    3      $90,000,$85,000,$80,000
The query returns
Mike    $90,000
       June    $90,000
because 90,000 is greater than 80,000 and 85,000

 Notice that Jack is not returned because his salary is greater than 3 other salaries.  Also note there are 5 employees with a salary less than Mike's and June's but the distinct keyword forces the count to 2.
nbewdwxp

nbewdwxp2#

如前所述,数字2周围的括号是无用的。等效查询可以是

SELECT * FROM 
(
  select sal, DENSE_RANK() OVER (ORDER BY sal) dr from employee
) emp
WHERE emp.dr = 3

小提琴
正如您在示例中看到的,您正在搜索雇员,其中雇员表中存在两个不同的较低工资。

相关问题