Link to home
Start Free TrialLog in
Avatar of KPax
KPax

asked on

Oracle MAX function

-- OK this works fine if I want to extract maxmum value from
-- group and by taht I mean that there is data such as departments and tehre are
-- veriuos salaries in those departments so MAX extracts the biggest one from each group
-- in this case DEPARTMENT_ID and that's it
SELECT DEPARTMENT_ID, MAX(SALARY) 
FROM EMPLOYEES 
GROUP BY DEPARTMENT_ID;

Open in new window


-- But what if I want to find single individual(s) with highest sallary?
-- and to dispaly his/her/their name and ID?
-- WITHOUT using JOIN just with selects or nested selects?

-- With following query MAX function displays ALL employess wityh their salaries

SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME,  MAX(SALARY) 
FROM EMPLOYEES 
GROUP BY EMPLOYEE_ID, FIRST_NAME, LAST_NAME;

Open in new window


User generated image
ASKER CERTIFIED SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KPax
KPax

ASKER

Thank you, that's the answer I was looking for.
Although I am not quite sure what was this supposed to do
select e.*, max(salary) over (order by employee_id) as max_salary
from employees e

Open in new window


this

select max(salary) from employees

-- and then use that to get the highest roller :

select * from employees where salary = (select max(salary) from employees)

Open in new window


makes perfect sense to me and it works!