[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Oracle MAX function

Posted on 2014-12-29
2
Medium Priority
?
263 Views
Last Modified: 2014-12-29
-- 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


Screen-Shot-12-29-14-at-08.23-PM.PNG
0
Comment
Question by:KPax
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 38

Accepted Solution

by:
Geert Gruwez earned 2000 total points
ID: 40522337
group by  and a max
the max is done per items in the group by

you designated the group by as employee_id, ...
the employee_id is unique (primary key), so the group by works on 1 element every time

the max wage of the employee itself, is the wage of the employee

the single individual (or equals) is the max salary over the total group

select max(salary) from employees

and then use that to get the highest roller :

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

you can alternatively add a highest salary as extra column

select e.*, max(salary) over (order by employee_id) as max_salary
from employees e
0
 

Author Closing Comment

by:KPax
ID: 40522362
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!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question