如何编写SQL查询以在Oracle中查找最高和最低薪水以及员工姓名

时间:2018-12-25 14:10:39

标签: sql oracle oracle11g aggregate-functions

我有一张员工表。请在表格结构下方找到数据。

enter image description here

我想找到最高薪水和最低薪水以及员工姓名。

预期的输出将是:

enter image description here

要找出最高薪水和员工姓名,我已编写此查询:

SELECT name,salary FROM  employee where salary
= (select max(salary) from employee);

为了找出最低工资和员工姓名,我编写了以下查询:

SELECT name,salary FROM  employee where salary
= (select min(salary) from employee);

但是我无法合并这两个查询。

有人可以指导我构建SQL查询,该查询将返回最高薪水和最低薪水以及员工姓名吗?

5 个答案:

答案 0 :(得分:2)

这是使用null的一种方法。此答案使CTE通过了一次,以找到employee表中最低和最高排名的薪水。然后,它仅子查询一次即可检索完整的匹配记录。

None

enter image description here

Demo

答案 1 :(得分:0)

您可以使用union allor

SELECT e.name, e.salary
FROM employee e
WHERE e.salary = (SELECT MAX(e2.salary) FROM employee e2) OR
      e.salary = (SELECT MIN(e2.salary) FROM employee e2);

答案 2 :(得分:0)

如果您希望它们全部归入一条记录中,请执行以下操作:

WITH t1 AS (
    SELECT name, salary FROM  employee where salary
    = (select max(salary) from employee) ),
t2 AS (    
    SELECT name, salary FROM  employee where salary
    = (select min(salary) from employee) )
SELECT
    t1.name AS MAX_NAME, t1.salaray AS MAX_SALARY, t2.name AS MIN_NAME, t2.salaray AS MIN_SALARY
FROM t1 LEFT JOIN t2 ON 1=1

这会将您的两个查询都放入通用表表达式(CTE,本质上就像您可以查询的表一样)。然后,将它们按任意true 1 = 1加入。然后,您只需选择四个值即可。

答案 3 :(得分:0)

您还可以使用self-join alike查询逻辑

with Employee(name, salary) as
(
 select 'Rick',3000 from dual union all
 select 'John',4000 from dual union all
 select 'Shane',3000 from dual union all
 select 'Peter',5000 from dual union all
 select 'Jackob',7000 from dual
)
 select name, salary
   from Employee
   join ( select max(salary) as max_sal, 
                 min(salary) as min_sal
           from Employee )
      on  salary in ( min_sal, max_sal )
  group by name, salary
  order by salary desc, name;

 NAME   SALARY
 ------ -------
 Jackob  7000
 Rick    3000
 Shane   3000

dbfiddle.uk demo

答案 4 :(得分:0)

如果您说的是代码,您将得到获得所有薪水为[{IN]最低(薪水)或最高(薪水)的员工::

SELECT e.name, e.salary
FROM employee e
WHERE e.salary in ( 
  select min(salary) sal from employee 
    union 
  select max(salary) from employee);

重要的是定义工资指数

 create index  employee_Idx on  employee(salary);

查询进行两次索引访问以获取最小值和最大值-下面计划中的第7行和第9行。 与使用此选择的值相比,NESTED LOOPS的执行是对salary进行索引访问,并通过ROWID进行表访问。

请注意,此优化会压迫只有极少数薪水的行(通常是这种情况-至少对于MAX;)

| Id  | Operation                       | Name         | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                |              |     3 |    66 |     6  (34)| 00:00:01 |
|   1 |  NESTED LOOPS                   |              |     3 |    66 |     6  (34)| 00:00:01 |
|   2 |   NESTED LOOPS                  |              |     3 |    66 |     6  (34)| 00:00:01 |
|   3 |    VIEW                         | VW_NSO_1     |     2 |    26 |     4  (50)| 00:00:01 |
|   4 |     SORT UNIQUE                 |              |     2 |     6 |     4  (50)| 00:00:01 |
|   5 |      UNION-ALL                  |              |       |       |            |          |
|   6 |       SORT AGGREGATE            |              |     1 |     3 |     2  (50)| 00:00:01 |
|   7 |        INDEX FULL SCAN (MIN/MAX)| EMPLOYEE_IDX |     1 |     3 |     1   (0)| 00:00:01 |
|   8 |       SORT AGGREGATE            |              |     1 |     3 |     2  (50)| 00:00:01 |
|   9 |        INDEX FULL SCAN (MIN/MAX)| EMPLOYEE_IDX |     1 |     3 |     1   (0)| 00:00:01 |
|* 10 |    INDEX RANGE SCAN             | EMPLOYEE_IDX |     1 |       |     0   (0)| 00:00:01 |
|  11 |   TABLE ACCESS BY INDEX ROWID   | EMPLOYEE     |     1 |     9 |     1   (0)| 00:00:01 |
------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

  10 - access("E"."SALARY"="SAL")