简体   繁体   中英

Max and Min sal with employee name in one query

I'm working on one SQL query.

Table name: employees .

I want to get the MAX and MIN sal with their employee names in SQL.

I know how to do with either MAX or MIN . But how can we do it both in one query?

I need a single row output like below:

e1.name AS MaxName, MAX(e1.sal) AS MaxSalary, e2.name AS MinName, MIN(e2.sal) AS MinSalary

Two ways:

Using Analytic function:

SQL> SELECT  MIN(ename) KEEP (DENSE_RANK FIRST ORDER BY sal) min_name,
  2          MIN(sal) AS min_sal,
  3          MAX(ename) KEEP (DENSE_RANK LAST ORDER BY sal) AS max_name,
  4          MAX(sal) AS max_sal
  5  FROM    emp;

MIN_NAME      MIN_SAL MAX_NAME      MAX_SAL
---------- ---------- ---------- ----------
SMITH             800 KING             5000

Using an In-line view :

SQL> WITH DATA AS
  2    ( SELECT MIN(sal) min_sal, MAX(sal) max_sal FROM emp
  3    )
  4  SELECT
  5    (SELECT e.ename FROM DATA t, emp e WHERE e.sal = t.min_sal AND ROWNUM =1
  6    ) min_name,
  7    (SELECT t.min_sal FROM DATA t, emp e WHERE e.sal = t.min_sal AND ROWNUM =1
  8   ) min_sal,
  9    (SELECT e.ename FROM DATA t, emp e WHERE e.sal = t.max_sal AND ROWNUM =1
 10   ) max_name,
 11    (SELECT t.max_sal FROM DATA t, emp e WHERE e.sal = t.max_sal AND ROWNUM =1
 12   ) max_sal
 13     FROM dual;

MIN_NAME      MIN_SAL MAX_NAME      MAX_SAL
---------- ---------- ---------- ----------
SMITH             800 KING             5000

In a single select:

SELECT  MIN( salary ) AS MinSalary,
        MIN( name ) KEEP ( DENSE_RANK FIRST ORDER BY salary ASC ) AS MinName,
        MAX( Salary ) AS MaxSalary,
        MAX( name ) KEEP ( DENSE_RANK LAST ORDER BY salary ASC ) AS MaxName
FROM    Employees;

The following solution works for MySQL, which was one of the tags you originally had when you posted your question.

You can perform a CROSS JOIN of the employees table against itself to find the max name/salary with a query which finds the min name/salary.

SELECT e1.name AS MaxName, MAX(e1.sal) AS MaxSalary,
    e2.name AS MinName, MIN(e2.sal) AS MinSalary
FROM employees e1 CROSS JOIN employees e2

Click the link below for a running demo. I actually include the name/salary pairs, though you can remove the names if you don't want them there.

SQLFiddle

Try something like:

select max(sal), min(sal), employee_id 
from employees
group by employee_id;

After that you can join it to get the name. Maybe you can group by name and id too.

Assuming that you are using Oracle, try this. Here first we are getting rownumber in ascending and descending order and then doing a cross join.

with employee(id,name,sal) as
(select 1,'a',1000 from dual union all
select 3,'c',1500 from dual union all
select 2,'b',2000 from dual)   --temp table to recreate the scenario
, enew as(
select e.*,row_number() over (order by sal) as salasc,row_number() over (order by sal desc) as saldesc from employee e
) --temp table to find the rownumber in ascending and descending order
--original query
select * from (select id as minsalid,name as minsalempname,sal as minsal from enew
where salasc=1)
cross join
(select id as maxsalemp,name as maxsalempname,sal as maxsal from enew
where saldesc=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM