如何从下表中检索最高和最低工资?

时间:2015-07-30 07:18:23

标签: sql oracle select oracle11g

我有员工表

 EMP_ID | F_NAME    | L_NAME  | SALARY | JOINING_DATE                 | DEPARTMENT
-----------------------------------------------------------------------------------
 101    | John      | Abraham | 100000 | 01-JAN-14 09.15.00.000000 AM | Banking
 102    | Michel    | Clarke  | 800000 |                              | Insaurance
 102    | Roy       | Thomas  | 70000  | 01-FEB-13 12.30.00.000000 PM | Banking
 103    | Tom       | Jose    | 600000 | 03-FEB-14 01.30.00.000000 AM | Insaurance
 105    | Jerry     | Pinto   | 650000 | 01-FEB-13 12.00.00.000000 PM | Services
 106    | Philip    | Mathew  | 750000 | 01-JAN-13 02.00.00.000000 AM | Services
 107    | TestName1 | 123     | 650000 | 01-JAN-13 12.05.00.000000 PM | Services
 108    | TestName2 | Lname%  | 600000 | 01-JAN-13 12.00.00.000000 PM | Insaurance

我想在oracle sql中找到上表中的最高和最低工资。 如果我做了

 select max(salary) from (select * from (select salary from employee) where   rownum <2);

它返回MAX(SALARY) = 100000,它应返回800000

如果我这样做

select max(salary)
  from (select * from (select salary from employee) 
         where rownum <3);

它返回MAX(SALARY) = 800000

如果我这样做

select min(salary)
  from (select * from(select salary from employee)
         where rownum < 2);

它会返回MIN(SALARY) = 100000,返回70000

此查询有什么问题?

什么是正确的查询?

8 个答案:

答案 0 :(得分:4)

您不需要所有这些子查询:

SELECT MAX(salary), MIN(salary)
FROM   employee

答案 1 :(得分:1)

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE employee ( EMP_ID,  F_NAME,  L_NAME,  SALARY,  JOINING_DATE, DEPARTMENT ) AS
          SELECT 101, 'John',      'Abraham', 100000, TIMESTAMP '2014-01-01 09:15:00', 'Banking'   FROM DUAL
UNION ALL SELECT 102, 'Michel',    'Clarke',  800000, NULL,                            'Insurance' FROM DUAL
UNION ALL SELECT 102, 'Roy',       'Thomas',   70000, TIMESTAMP '2013-02-01 12:30:00', 'Banking'   FROM DUAL
UNION ALL SELECT 103, 'Tom',       'Jose',    600000, TIMESTAMP '2014-02-03 01:30:00', 'Insurance' FROM DUAL
UNION ALL SELECT 105, 'Jerry',     'Pinto',   650000, TIMESTAMP '2013-02-01 12:00:00', 'Services'  FROM DUAL
UNION ALL SELECT 106, 'Philip',    'Mathew',  750000, TIMESTAMP '2013-01-01 02:00:00', 'Services'  FROM DUAL
UNION ALL SELECT 107, 'TestName1', '123',     650000, TIMESTAMP '2013-01-01 12:05:00', 'Services'  FROM DUAL
UNION ALL SELECT 108, 'TestName2', 'Lname%',  600000, TIMESTAMP '2013-01-01 12:00:00', 'Insurance' FROM DUAL;

查询1 - 查找最高n的工资

SELECT *
FROM   (
  SELECT salary
  FROM   employee
  ORDER BY salary DESC
)
WHERE  rownum <= 3 -- replace with the number of salaries you want to retrieve.

<强> Results

| SALARY |
|--------|
| 800000 |
| 750000 |
| 650000 |

查询2 - 查找最低n的工资

SELECT *
FROM   (
  SELECT salary
  FROM   employee
  ORDER BY salary ASC
)
WHERE  rownum <= 3 -- replace with the number of salaries you want to retrieve.

<强> Results

| SALARY |
|--------|
|  70000 |
| 100000 |
| 600000 |

答案 2 :(得分:0)

对于查询返回的每一行,ROWNUM伪列返回一个数字,表示Oracle从一个表或一组连接行中选择行的顺序。选择的第一行的ROWNUM为1,第二行为2,依此类推。

所以在你的情况下:

select max(salary) from (select * from (select salary from employee) where   rownum <2);

此查询将返回

101    John     Abraham 100000  01-JAN-14 09.15.00.000000 AM    Banking

仅将此行作为输出...因此最大值仅为100000。

select max(salary) from (select * from (select salary from employee) where  rownum <3);

此查询将从您的表中前两行,即

101    John     Abraham 100000  01-JAN-14 09.15.00.000000 AM    Banking
102    Michel   Clarke  800000                                Insaurance

因此最高工资为800000。

类似地,

select min(salary)from (select * from(select salary from employee)where rownum<2);

只会选择第一行

select min(salary)from (select * from(select salary from employee)where rownum<2);

所以最低工资将是100000。

P.S。 :您可以像这样编写查询:

select max(salary) from  employee where rownum<[n];

其中n将是您要限制查询返回的行数的ROWNUM

答案 3 :(得分:0)

试一试:

select *
from (
  select T.*, rownum RRN
  from (
    select salary
    from employee
    order by salary desc) T)
where RRN < 3

答案 4 :(得分:0)

所以你想要第二高和第二低的薪水?看看这个

select max(salary), min(salary) from employee
where salary < (select max(salary) from employee)
and salary > (select min(salary) from employee)
;

答案 5 :(得分:0)

1) For lowest salary.
select * from (
               select empno,job,ename,sal 
                   from emp order by sal) 
                          where rownum=1;

2) For Highest salary.
select * from (
               select empno,job,ename,sal 
                   from emp order by sal desc)
                          where rownum=1;

答案 6 :(得分:0)

我不知道为什么要进行复杂的查询,您可以简单地编写以下代码并获得相同的结果:

select salary
from employees
where rownum <=3
order by salary desc;

答案 7 :(得分:0)

您可以通过以下查询来解决您的问题:

最高工资:

Select * from Employee(Select salary from Employee ORDER BY salary DISC) where rownum=1;

最低工资:

Select * from Employee(Select salary from Employee ORDER BY salary) where rownum=1;

第二高薪:

Select MAX(Salary) from Employee 
Where Salary < (Select MAX(Salary) from employee);

第二低的工资:

Select MIN(Salary) from Employee 
Where Salary > (Select MIN(Salary) from employee);