0

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
6
  • 4
    decide database tag first Nov 19, 2015 at 7:44
  • 1
    You could use UNION. Nov 19, 2015 at 7:45
  • Do you want your result as two records, or one record with two columns? Nov 19, 2015 at 7:46
  • @TimBiegeleisen, I need one record with two columns. As two records i can do it by union..!! :)
    – drill
    Nov 19, 2015 at 7:48
  • 4 bits of information in two columns? What should the result look like?
    – Strawberry
    Nov 19, 2015 at 8:02

6 Answers 6

2

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
6
  • I'm looking for like this e1.name AS MaxName, MAX(e1.sal) AS MaxSalary, e2.name AS MinName, MIN(e2.sal) AS MinSalary. Just one record
    – drill
    Nov 19, 2015 at 8:19
  • @drill Done. See the update. Using an inline view. Please mark it as answered, would help others too! Nov 19, 2015 at 8:35
  • you did that !! Thanks Alot !!
    – drill
    Nov 19, 2015 at 9:27
  • If there are two people who have the same min/max salary then the updated query will error with ORA-01427: single-row subquery returns more than one row.
    – MT0
    Nov 19, 2015 at 9:42
  • @MTO To avoid the ORA-01427 in the in-line query is to restrict the rows using ROWNUM. Updated with corrected query. That's the disadvantage of in-line view. Also not good in performance. FIRST and LAST are appropriate. Nov 19, 2015 at 9:53
1

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;
1
  • It is not handling duplicate salaries properly.Two or more employees with maximum salary and/or two or more employees with minimum salary, it will display partial result.
    – Bacteria
    Dec 25, 2018 at 13:48
0

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)
0

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

3
  • @Tim, Not working. ORA-00937 "not a single-group group function"
    – drill
    Nov 19, 2015 at 8:06
  • Why didn't you tag your question with Oracle? Nov 19, 2015 at 8:07
  • Am I going in wrong direction ? query requirement makes sense or not ?
    – drill
    Nov 19, 2015 at 8:10
0

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.

0
with min_max_age as
  (
    select dept,
    min(age) as  min_Age,
    max(age) as max_age
    from Employees
    group by dept
  )
select a.name, a.dept, a.age from Employees a join min_max_age b
on (a.age = b.min_Age and a.dept = b.dept)
or (a.age = b.max_age and a.dept = b.dept)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.