Solved

Oracle MAX function

Posted on 2014-12-29
2
241 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 37

Accepted Solution

by:
Geert Gruwez earned 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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.

688 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