Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question.

Asked: March 18, 2018 - 2:22 am UTC

Last updated: March 21, 2018 - 11:06 am UTC

Version: 12c

Viewed 1000+ times

You Asked

Hi, Look at here..... Find highest salary in each department without using MAX function.
Note:-
Use a single SELECT statement only.
For an added complexity (optional): try not using ANY functions at all (neither group, nor analytic, not even scalar)

and Connor said...

select deptno, abs(min(-sal)) from emp group by deptno

Not much complexity there

Rating

  (5 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

One possibility

lh, March 18, 2018 - 2:05 pm UTC

something like:
select
sal
from (
select sal from emp order by sal desc
) where rownum =1

Connor McDonald
March 19, 2018 - 4:46 am UTC

You have TWO select statements there.


A reader, March 19, 2018 - 7:25 am UTC

But Oracle doesn't mention the max(-Column_name) in PLSQL documentation?
Why?

Just in case all numbers are negative

Stew Ashton, March 19, 2018 - 10:11 am UTC

with data as (select -1 n from dual)
select max(n),
  abs(min(-n)), 
  -min(-n)
from data;


    MAX(N) ABS(MIN(-N))   -MIN(-N)
---------- ------------ ----------
        -1            1         -1

Connor McDonald
March 21, 2018 - 11:05 am UTC

When the column "salary" goes negative, I've got other concerns :-)

To: a reader

Stew Ashton, March 19, 2018 - 10:18 am UTC

You say "But Oracle doesn't mention the max(-Column_name) in PLSQL documentation?
Why?"

First of all, you mean min not max.

Second, this is a SQL statement, not PLSQL.

Third, the documentation talks about min, and it talks about the minus sign "-". Putting the two together is our job.

Fourth, are you a plant by Russians to destroy Oracle by frustrating the askTOM team into shutting down this site? FAKE QUESTION!
Connor McDonald
March 21, 2018 - 11:05 am UTC

:-)

Now you tell me.

Chuck Jolley, March 19, 2018 - 8:42 pm UTC

After twenty years writing Oracle sql I learn that I can just stick a minus sign on the front of a numeric column to negate it?
I'll bet I've written -1 * col a thousand times to convert accounting entries to credit values.
Learn something new every day.

Connor McDonald
March 21, 2018 - 11:06 am UTC

That's why I love working on AskTOM

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library