Solved

Oracle MAX function

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

Accepted Solution

by:
Geert Gruwez earned 500 total points
Comment Utility
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
Comment Utility
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.

Join & Write a Comment

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now