Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在下面的SQL查询中,(2)是什么意思_Sql_Oracle - Fatal编程技术网

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

在下面的SQL查询中,(2)是什么意思,sql,oracle,Sql,Oracle,有人请解释为什么在下面的查询中使用2 select * from employee e where(2) = (select count(distinct(e1.sal)) from employee e1 where e.sal > e1.sal); 该查询返回其工资高于其他两个工资的所有员工。括号有误导性,不需要 select * from employee e where 2 = (select count(distinct(e1

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

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

该查询返回其工资高于其他两个工资的所有员工。括号有误导性,不需要

select * from employee e 
where 2 = (select count(distinct(e1.sal))
            from employee e1
            where e.sal > e1.sal);
例如,给定以下员工数据:

Employee Salary Joe $80,000 Kate $80,000 Lee $85,000 Chris $85,000 Matt $85,000 Mike $90,000 June $90,000 Jack $100,000
如前所述,数字2周围的括号是无用的。等价的查询可以是

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

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

意味着您希望通过关联查询从整个员工中获得两个不同的工资。通常情况下,条件写为其中一些条件=5,但是你也可以反过来写:WHERE 5=某个条件。与WHERE select相同…=2 2括号中的2是无用的。因此,在本例中,您选择的是最低工资中第三个的人,即有两个其他员工的工资值低于他/她的工资值。我应该说是员工,而不是个人,因为可能有多个员工 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.
SELECT * FROM 
(
  select sal, DENSE_RANK() OVER (ORDER BY sal) dr from employee
) emp
WHERE emp.dr = 3