Solved

Oracle MAX function

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
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.

806 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