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
Similar Messages
-
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). -
How to find 'n' th maximum salary in employees table
how to find 'n' th maximum salary in employees table using stored procedure............
Depends on your unmentioned database version.
In general you're asking for a Top-N query.
See this article:
http://www.oracle.com/technetwork/issue-archive/2007/07-jan/o17asktom-093877.html
In 11g you have http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/functions114.htm#CJAFEJBE
an analytic function
http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/functions004.htm#SQLRF06174 -
How to get second maximum salary from employee table(sql query)
how to get second maximum salary from employee table(sql query)
dude there is no matter of structure .........that user already said its from employee table ...............its basic table in sql and there is no need to specify the table structure
.........i think u got my point I think you are the one who didn't understand Sarma's point.
Give a man a fish and you feed him once. Teach a man how to fish and you feed him a life long.
>
and the query is
select max(sal) from emp where sal<(select max(sal)
from emp);
this will give the 2nd max salary from the emp tableBtw: You solution is bad, because it needs to scan and sort the table emp twice. And a better solution has been given already.
Message was edited by:
Sven W. - reordered statements -
To find the lowest salary from a table
hello guys,
i need to find the lowest salary from a table. whats the easy way to do this.How do you handle ties?
If you want multiple rows returned when there are multiple people with the same salary
SELECT *
FROM(
SELECT a.*,
rank() over (order by salary desc ) rnk
FROM your_table a)
WHERE rnk = 1If you want to handle ties differently, use a different analytic function. Probably dense_rank or row_number.
Justin -
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=3chris227 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. -
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 -
How to find top three salary of emp table?
Hi,
I am trying to get top 3 salaries using MAX() in sql but it is only showing 1 row not 3.The following qury showing only the 3rd highest.
SELECT MAX(Sal) ThirdHighest FROM emp
WHERE Sal < (SELECT MAX(Sal) FROM emp
WHERE Sal =(SELECT MAX(Sal) FROM emp
WHERE Sal <(SELECT MAX(Sal) FROM emp)))
I dont want to use the following method.
SELECT department_id dept, first_name || ' ' || last_name name, salary
FROM (
SELECT department_id, first_name, last_name,
MAX(salary) OVER (PARTITION BY department_id) dept_max_sal, salary
FROM employees e
) WHERE salary = dept_max_sal;
Kindly help to create query using only "max()" for top 3 salaries.947459 wrote:
Hi,
I am trying to get top 3 salaries using MAX() in sql but it is only showing 1 row not 3.The following qury showing only the 3rd highest.
SELECT MAX(Sal) ThirdHighest FROM emp
WHERE Sal < (SELECT MAX(Sal) FROM emp
WHERE Sal =(SELECT MAX(Sal) FROM emp
WHERE Sal <(SELECT MAX(Sal) FROM emp)))
I dont want to use the following method.
SELECT department_id dept, first_name || ' ' || last_name name, salary
FROM (
SELECT department_id, first_name, last_name,
MAX(salary) OVER (PARTITION BY department_id) dept_max_sal, salary
FROM employees e
) WHERE salary = dept_max_sal;
Kindly help to create query using only "max()" for top 3 salaries.Why? Why not use what Oracle provides for doing top N queries?
SQL> ed
Wrote file afiedt.buf
1 select empno, ename, sal
2 from (
3 select empno, ename, sal, dense_rank() over (partition by 1 order by sal desc) as r from emp
4 )
5* where r <= 3
SQL> /
EMPNO ENAME SAL
7839 KING 5000
7788 SCOTT 3000
7902 FORD 3000
7566 JONES 2975Using just the MAX function alone will make it harder for you and is completely unnecessary. -
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> -
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. -
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. -
Salary of employees of respective manager
i have manager_id,last_name,salary from employees table i need to print the managers name and the lowest salry of the respective employee working under the manager
think about it. You want the lowest salary of an employee with this manager. Do we have a function that returns the lowest value? Why yes, it's MIN.
So we want our where clause to include
and a.salary = (SELECT min(salary) from employees c from this manager (a))
I leave you to do the simple join from c to a. -
Retrive the second-highest value from the table using query.
Hi,
I am using Oracle 8i...
I have to retrieve the second-highest salary from the table...for this I need to write a SQL..Can anybody please help me to figure out this problem..Thanks in advance..
smithaUsing analytic functions
SELECT *
FROM (SELECT other_columns,DENSE_RANK() OVER(ORDER BY salary DESC) sal_rank
FROM table)
WHERE sal_rank = 2
Using Order by and rownum
SELECT *
FROM (SELECT other_columns,rownum sal_rank
FROM (SELECT other_columns,salary
FROM table
ORDER BY salary DESC))
WHERE sal_rank = 2TTFN
John -
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.
Maybe you are looking for
-
I want to see the result of my editing on the second monitor for broadcasting
-
How to check how much my macbook pro has left and also why does my battery charger get really hot?
-
I have requirement like this in Cust Lamp ActualCost Selliing Price Revenue No.ofSupplied Sales Revenue Maruti xyz 100 150 50 1lakh 150 50 This data I will be getting from Co Now Mar
-
Trouble getting started with AX
I'm sorry if this issue has been addressed early; in perusing earlier posts I couldn't find exactly what I was looking for. I have purchased an AX and want to inlcude it in my home wireless network, which uses a Neatgear wireless router. I've plugged
-
Hi, I have an HP Pavilion 17-e031sa Notebook PC purchased only 2-weeks ago and running Windows 8. The ctrl key is not working as it should. I would typically use it together with the left mouse key to highlight non-consecutive items for copying, bu