Finding nth highest salary
Hi guys , I am trying to find the nth highest salary
using the below query , is there any case in which the below query can fail
select
* from (
select
distinct rownum rn,salary from emp_mgr order by rownum) t where t.rn=3
chris227 wrote:
Sory, may be i am blind, dont see it yet.
Doesnt the Nth high paid employee earns the Nth highest salary?
No. Multiple employees can earn same salary. Look at EMP table:
SQL> select ename,sal from emp order by sal desc;
ENAME SAL
KING 5000
FORD 3000
SCOTT 3000
JONES 2975
BLAKE 2850
CLARK 2450
ALLEN 1600
TURNER 1500
MILLER 1300
WARD 1250
MARTIN 1250
ENAME SAL
ADAMS 1100
JAMES 950
SMITH 800
Highest salary 5000. Second highest salary is 3000. Third highest salary is 2975. Now look what NTH_VALUE returns:
SQL> select distinct sal
2 from (select nth_value(sal,&n) over(order by sal desc) sal from emp)
3 /
Enter value for n: 1
old 2: from (select nth_value(sal,&n) over(order by sal desc) sal from emp)
new 2: from (select nth_value(sal,1) over(order by sal desc) sal from emp)
SAL
5000
SQL> /
Enter value for n: 2
old 2: from (select nth_value(sal,&n) over(order by sal desc) sal from emp)
new 2: from (select nth_value(sal,2) over(order by sal desc) sal from emp)
SAL
3000
SQL> /
Enter value for n: 3
old 2: from (select nth_value(sal,&n) over(order by sal desc) sal from emp)
new 2: from (select nth_value(sal,3) over(order by sal desc) sal from emp)
SAL
3000
SQL> /
Enter value for n: 4
old 2: from (select nth_value(sal,&n) over(order by sal desc) sal from emp)
new 2: from (select nth_value(sal,4) over(order by sal desc) sal from emp)
SAL
2975
SQL>
SY.
Similar Messages
-
I need a well tuned query to find the nth highest salary from emp table.
I have one but no idea if it is tuned one.
select min(sal) from (select sal,rownum from (select sal from (select distinct sal from emp order by sal desc)) where rownum <=n) ;You can use analytic functions.
select *
from (
select e.*, row_number() over(order by sal desc nulls last) rno
from emp e
where rno = 5 --<<-- replace 5 with N
I need a well tuned query to find the nth highest salary from emp table.This totally depends on how you have setup your database. -
Nth Highest Salary from EMP table , need explaination
I am new to the database world and hence learning queries at the initial stages. I want to retrieve the nth highest salary from the EMP table. I was not able to find out the answer by myself , so I searched on google. The following was the answer i got :
SELECT DISTINCT (a.sal) FROM EMP A WHERE &N = (SELECT COUNT (DISTINCT (b.sal)) FROM EMP B WHERE a.sal<=b.sal);
Enter value for n: 2
SAL
3700
Suppose the table contains salary in the following order :
SAL
2000
3000
3700
4000
3700
2000
So how come I will get the correct answer with the abov query ? I am not able to understand the part --- WHERE &N = (SELECT COUNT (DISTINCT (b.sal)) FROM EMP B WHERE a.sal<=b.sal);
Specially the condition WHERE a.sal<=b.sal is very confusing to me. Can anyone help me please to understand the same.user12328699 wrote:
I am new to the database world and hence learning queries at the initial stages. I want to retrieve the nth highest salary from the EMP table. I was not able to find out the answer by myself , so I searched on google. The following was the answer i got :
SELECT DISTINCT (a.sal) FROM EMP A WHERE &N = (SELECT COUNT (DISTINCT (b.sal)) FROM EMP B WHERE a.sal<=b.sal);
Enter value for n: 2
SAL
3700
Suppose the table contains salary in the following order :
SAL
2000
3000
3700
4000
3700
2000
So how come I will get the correct answer with the abov query ? I am not able to understand the part --- WHERE &N = (SELECT COUNT (DISTINCT (b.sal)) FROM EMP B WHERE a.sal<=b.sal);
Specially the condition WHERE a.sal<=b.sal is very confusing to me. Can anyone help me please to understand the same.There is many better way to get the nth highest salary
But still if you want to understand the same.
Assuming the data is sorted in ascending order, then
1) a.sal=2000, so a.sal<=b.sal results in count as 3 (not equal to user input)
2) a.sal=3700, so a.sal<=b.sal results in count as 2 (equal to user input)
3) a.sal = 4000 , so a.sal<=b.sal results in count as 1 (not equal to user input)
Hence the answer is 3700
Regards
Anurag -
Oracle 9i SQL: Find nth highest
Hi,
Please help me to understand the logic behind the following query to find the nth highest:
select empNo, salary
from emp a
where (n-1) = (select count(*) from (select distinct salary from emp)b where b.salary > a.salary )
Thanks,
priya ranjanRead the following marvellous link:
http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html
Greetings,
Simon -
To find 2nd highest salary in employee table
Hi,
I want to know how to calculate the 2nd highest salaried employee details.
rownum "=" sign cannot be used i suppose and
with and without using orderby clause.
both the methods needed.
cheers,With order by
select * from
( select q.*, rownum as rn from
( select * from emp order by sal desc ) q
where rn=2
/With analytics:
select empno , sal
from ( select empno , sal
, dense_rank () over (order by sal desc) as sal_rank
from emp )
where sal_rank = 2Notice that DENSE_RANK() might give us a different result compared to RANK() and both give us a different result from the first example.
Cheers, APC -
How will i get the n'th highest salary????????
Hi
plz can anybody tell me the query to find nth highest salary from emp table???
Thanx
Gagantry this
SELECT employee_name, salary,
RANK() OVER (order by salary) AS ranking
FROM employee) empor to get the particular ranking
SELECT emp.employee_name, emp.salary
FROM
(SELECT employee_name, salary,
RANK() OVER (order by salary) AS ranking
FROM employee) emp
WHERE emp.ranking = <rank number> -
Pls help me in finding highest salary of employee.
can anybody give me...
the general query...for finding out nth highest salary of the employee in a particular department.....
for example, if i want to find out 2nd highest salary or some 5th highest salary of the employee.....
i want query with a littlebit explanation also...i
cannot do the sumation of those value then the
negative value that being re-enter become 0. how can
i make the value to become another positive value?Are you doing the `sumation' during the data entry (loop)? If so, make sure you only include valid value in the count (i.e. positive integers). -
Hi,
What is the SQL query to find the 1st highest salary.
2nd Highest salary in the emp table.
likewise nth highest salary employee records.
Can anyone please give me the queries for the above scenarios.
ThanksThere are many nethods, some examples
Using group function:
SQL> l
1 SELECT ename,sal FROM emp e1
2 WHERE &val >= (SELECT COUNT(*) FROM emp e2
3* WHERE e1.sal<=e2.sal)
SQL> /
Enter value for val: 1
old 2: WHERE &val >= (SELECT COUNT(*) FROM emp e2
new 2: WHERE 1 >= (SELECT COUNT(*) FROM emp e2
ENAME SAL
KING 5000Using in-line view and rownum:
SQL> ed
Wrote file afiedt.buf
1 SELECT * FROM (SELECT ename,sal FROM emp ORDER BY SAL desc)
2* WHERE rownum<2
SQL> /
ENAME SAL
KING 5000Using analytical function ROW_NUMBER
SQL> ed
Wrote file afiedt.buf
1 SELECT * FROM (SELECT ename,sal,ROW_NUMBER() OVER(ORDER BY sal desc) rn FROM emp )
2* WHERE rn=1
SQL> /
ENAME SAL RN
KING 5000 1Also you can use several other analytical functions as mentioned by Kartick. Search and read on the internet to find what is most suitable way and why!.
http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:2853107469873
http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:12759079666984 -
hi, i am kind of new to sql. i have a table with employee name and salary.
i would like to find the name of the 2nd highest salary? also i like to get the name of the 5th highest salary
here is my data
INSERT INTO er4 VALUES('john', 1000, 123);
INSERT INTO er4 VALUES('john2', 3000, 234);
INSERT INTO er4 VALUES('john3', 1000, 223);
INSERT INTO er4 VALUES('john4', 4000, 123);
INSERT INTO er4 VALUES('john5', 8000, 023);
INSERT INTO er4 VALUES('john6', 9000, 023);
INSERT INTO er4 VALUES('john7', 8000, 723);
you can see in the data that there are two employee with highest second salary. i would like to get either one but i want my query to return one row
can somebody help me write a query to get the name and salary of 2nd highest and name and salary of 5th highestHi,
Part of learning about functions is learning what they are good for, and what they are not good for.
As Justin said, MAX is not good for this task.
If you were interested in just the 1st highest salary, you possibly could use MAX like this:
SELECT *
FROM er4
WHERE salary = (
SELECT MAX (salary)
FROM er4
AND ROWNUM = 1 -- in case of a tie, pick one row arbitrarily
;But even if the goal was to find the highest salary, I would use ROW_NUMBER, like Justin did.
Why? The following scenario happens all the time in real life. The client for whom you wrote this comes back in 3 months and says: "That query you wrote for the highest salary works great! We've found it so useful, we'd like to do something similar to find the 2nd highest salary, or the 5th highest, or the top 5, or the next 5." ROW_NUMBER is flexible enough to do all these things. When you have to solve one problem, you'll save time (in the long run) if you can foresee, or just guess, what similar problems you might need to solve later, and write something for today's problem that can easily be adapted for tomorrow's potential problem. -
How to find the third highest salary by distinct the salary from deptno20,from employee table
how to find the third highest salary by distinct the salary from deptno20,by using employee table
You already asked this question, a half hour earlier:
https://forums.oracle.com/thread/2569985
and received a reply.
Do not multi-post to the forums.
These are user-to-user forums and everyone is posting because they volunteer to post and not because they get paid to post. Everyone else is paid exactly the same as you are paid to post here. When you multi-post is makes you appear as impatient and expecting instantaneous answers.
That is poor forum etiquette, approaching behavior similar to spamming the forums.
This duplicate post is locked. -
How to find the third highest salary from deptno20,from employee table
how to find the third highest salary from deptno20,from employee table
SELECT *
FROM emp;
EMPNO
ENAME
JOB
MGR
HIREDATE
SAL
COMM
DEPTNO
7369
SMITH
CLERK
7902
12/17/1980
1000
3
20
7499
FEDERAL
SALESMAN
7654
2/20/1981
2000
4
30
7521
WARD
SALESMAN
7698
2/22/1981
3000
4
30
7566
JONES
MANAGER
7839
4/2/1981
4000
3
20
7839
MARTIN
SALESMAN
7698
9/28/1981
5421
4
30
7698
BLAKE
MANAGER
7698
5/1/1981
6222
4
30
7782
CLARK
MANAGER
7839
6/9/1981
5222
2
10
7788
SCOTT
ANALYST
7566
12/9/1982
5463
3
20
7839
KING
PRESIDENT
7902
11/17/1981
8543
2
10
7844
TURNER
SALESMAN
7698
9/8/1981
2124
4
30
7876
ADAMS
CLERK
7788
1/12/1983
2125
3
20
7900
JAMES
CLERK
7698
12/3/1981
5462
4
30
7902
FORD
ANALYST
7566
12/3/1981
2132
3
20
7934
MILLER
CLERK
7782
1/23/1982
5165
2
10
SELECT sal, deptno
FROM (SELECT a.*,
DENSE_RANK () OVER (PARTITION BY deptno ORDER BY sal DESC) r
FROM emp a
---- ORDER BY sal DESC
WHERE r = 3;
SAL
DEPTNO
5421
30
5165
10
2132
20
Regards ,
Friend
Message was edited by: MostWanted!!!! -
How to find record having second highest salary
Hello friends
I ve a table empsal
it has 2 columns name,salary
i need to find out name of employee with second highest salary.
how shud i ..pls help...
thks
sonal...in the doc
create or replace type SecondMaxImpl as object
max NUMBER, -- highest value seen so far
secmax NUMBER, -- second highest value seen so far
static function ODCIAggregateInitialize(sctx IN OUT SecondMaxImpl)
return number,
member function ODCIAggregateIterate(self IN OUT SecondMaxImpl,
value IN number) return number,
member function ODCIAggregateTerminate(self IN SecondMaxImpl,
returnValue OUT number, flags IN number) return number,
member function ODCIAggregateMerge(self IN OUT SecondMaxImpl,
ctx2 IN SecondMaxImpl) return number
create or replace type body SecondMaxImpl is
static function ODCIAggregateInitialize(sctx IN OUT SecondMaxImpl)
return number is
begin
sctx := SecondMaxImpl(0, 0);
return ODCIConst.Success;
end;
member function ODCIAggregateIterate(self IN OUT SecondMaxImpl, value IN number) return number is
begin
if value > self.max then
self.secmax := self.max;
self.max := value;
elsif value > self.secmax then
self.secmax := value;
end if;
return ODCIConst.Success;
end;
member function ODCIAggregateTerminate(self IN SecondMaxImpl,
returnValue OUT number, flags IN number) return number is
begin
returnValue := self.secmax;
return ODCIConst.Success;
end;
member function ODCIAggregateMerge(self IN OUT SecondMaxImpl, ctx2 IN SecondMaxImpl) return number is
begin
if ctx2.max > self.max then
if ctx2.secmax > self.secmax then
self.secmax := ctx2.secmax;
else
self.secmax := self.max;
end if;
self.max := ctx2.max;
elsif ctx2.max > self.secmax then
self.secmax := ctx2.max;
end if;
return ODCIConst.Success;
end;
end;
CREATE OR REPLACE FUNCTION SecondMax (input NUMBER) RETURN NUMBER
PARALLEL_ENABLE AGGREGATE USING SecondMaxImpl;
SELECT deptno,SecondMax(sal)
FROM emp
GROUP BY deptno;
DEPTNO SECONDMAX(SAL)
10 2450
20 3000
30 1600 -
How to retrieve 5th highest salary in a single statement
Hi,
How can we retrieve 5th highest salary in a single statementAnother method
select top 1
OrderID
, Quantity
from
select top 5
OrderID
, Quantity = sum (Quantity)
from
[Order Details]
group by
OrderID
order by
2 desc
) q
order by
2 asc
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence -
How to find the avg salary of first 3 employees based on each department?
how to find the avg salary of first 3 employees based on each department?
Hi,
Do you mean something like this?
with a as
select
deptno
,sal
,row_number() over ( partition by deptno order by sal desc) rn
from
emp
select
deptno
,round(avg(sal)) avg_sal_of_top_3
from
a
where
rn <= 3
group by
deptno
order by
deptno
DEPTNO AVG_SAL_OF_TOP_3
10 2917
20 2992
30 1983
If not please give more details.
Regards,
Peter -
How to get highest salary from salary column in sharepoint list
Hello,
I have one custom list in which there is one salary column so I want to get the highest salary.Can we do this OOTB or by using custom code by adding webpart(using CAML query).
Thanks,http://stackoverflow.com/questions/516073/max-query-using-caml
<Query>
<OrderBy>
<FieldRef Name="particularcolumn" Ascending="FALSE" />
</OrderBy>
</Query>
http://stackoverflow.com/questions/8383616/caml-query-on-a-sharepoint-2010-list
http://sharepoint.stackexchange.com/questions/16955/how-to-select-max-field-in-caml-query
If this helped you resolve your issue, please mark it Answered
Maybe you are looking for
-
Need node-level access in java.util.LinkedList... possible?
Hi, I require access to the nodes not the elements, in a java.util.LinkedList. Is this possible, or will I have to write my own implementation. Thanks Grape
-
New Authorization objects When Adding New tcodes
Hi Guys I have two Identical R3 Productiosn Systems One is Called Prd and the Othe RPP. When Going into Pfcg on PRD and adding A tocde I.e Mi02. It already has mi01 and mi03.the authorization tab chnages from green to Yellow,.When Going into The Aut
-
JTree Sub Folder of Tree Expanding Incorrectly
The first time I try to expand a node It gets Expanded. But then when I try to expand the subfolders It does not happen. I am simply not understanding how to go about. The program based on the lines of the 2nd Example of Java Swing 2nd edition(Mathew
-
An error occured while creating the original attribute....
Hello Gurus, I am getting an error message when I try to save a document using CV01N. It says: "An Error occured while creating the original attribute for XXX" Can someone please help me here ? Regards, Rajesh.
-
MacBook Pro and Studio 2 + other software compatibility
I wanted to buy a Macbook Pro 17" because I need to run the latest software for Studio 2 and the new Photoshop CS3 + latest version of After Effects, and I'm also never home so I can't buy a mac that's a tower like a g5. I was told that it was a bad