473,322 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Find the 3th,4th ,5th........... largest salary

I know how how to retrive the largest & second largest salary.
tell me how to retrive the 3,4,5,...............Largest salary.

Create table empl(empid int, empname varchar,salary int)
insert into empl values(1,'A',100)
insert into empl values(2,'B',200)
insert into empl values(3,'C',300)
insert into empl values(4,'D',400)
insert into empl values(5,'E',500)
insert into empl values(6,'F',600)

Foll. Query will retrive the 2nd largest salary-
select top 1 * from empl where salary<(select MAX(salary)
from empl)order by salary desc
Apr 19 '08 #1
4 7487
siva538
44
I know how how to retrive the largest & second largest salary.
tell me how to retrive the 3,4,5,...............Largest salary.

Create table empl(empid int, empname varchar,salary int)
insert into empl values(1,'A',100)
insert into empl values(2,'B',200)
insert into empl values(3,'C',300)
insert into empl values(4,'D',400)
insert into empl values(5,'E',500)
insert into empl values(6,'F',600)

Foll. Query will retrive the 2nd largest salary-
select top 1 * from empl where salary<(select MAX(salary)
from empl)order by salary desc
Expand|Select|Wrap|Line Numbers
  1. select top 1 * from  
  2. (select top 4 * FROM empl 
  3.  order by salary desc) e
  4. order by salary 
  5.  
This is for 4th highest salary. you can replace 4 with any number you want by the number for that much highest salary
Apr 19 '08 #2
select e1.sal, e1.name from emp e1
where (N-1) = (select count(distinct e2.sal) from emp e2 where e1.sal > e2.sal
from emp e2)

Where N=Nth highest salary
Apr 21 '08 #3
deepuv04
227 Expert 100+
I know how how to retrive the largest & second largest salary.
tell me how to retrive the 3,4,5,...............Largest salary.

Create table empl(empid int, empname varchar,salary int)
insert into empl values(1,'A',100)
insert into empl values(2,'B',200)
insert into empl values(3,'C',300)
insert into empl values(4,'D',400)
insert into empl values(5,'E',500)
insert into empl values(6,'F',600)

Foll. Query will retrive the 2nd largest salary-
select top 1 * from empl where salary<(select MAX(salary)
from empl)order by salary desc
Hi,
another way of getting nth record is. first give ranking to each row and get the
record you want
ex:
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM
  3.     ( select DENSE_RANK() OVER (ORDER BY Salary DESC) AS Rank, * from empl ) AS E1
  4. WHERE [Rank] IN (3,4,5)    
  5.  
DENSE_RANK() is the gives ranking for the rows

thanks
Apr 21 '08 #4
Approach 1 using DENSE_RANK()


Consider following details
table: Emp
Column: name, Salary


Expand|Select|Wrap|Line Numbers
  1. (SELECT name , salary , DENSE_RANK() over (order by salary desc) AS Rowno FROM emp) a 
  2. Where a.Rowno=3
Above example is for getting third largest salary.


Appraoch 2: using subquery


Expand|Select|Wrap|Line Numbers
  1. SELECT MIN(Salary) from EMP where salary in 
  2. (SELECT TOP 3 salary from emp e order by salary desc)
Nov 13 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Rom | last post by:
I'm a bit confused as to how the STL set works with the <setname>.insert() and <setname>.find() functions Compiler used : CodeWarrior 5.0 My intial interpretation was that I needed to overload...
5
by: Joerg Battermann | last post by:
Hello there, I have a custom type defined via Public Class Requirement Public IDNumber As Integer Public Name As String Public Description As String Public VersionPlanAttributes As New _
1
by: sallyk57 | last post by:
Help with following Programs: Write two programs one where the performance rating here shoud be entered as a int where Excellent =1, Good= 2, Poor=3. an employee who is rated excellent will...
22
by: ranjitkumar | last post by:
Hi everybody, create table employee { empno char(5) primay key, name varchar2(30), salary number(5,2) }; 1. For the above table how to find the employee with the third highest...
5
by: james121285 | last post by:
This is to calculate an employees tax and pension. The salary is input from the keyboard. The NI contribution is calculated as 6% of the gross salary. The pension contribution is calculated as 2%...
2
by: santoshsri | last post by:
Hello, Is there any by which I can write a SQL to get the 4th largest salary from a table without using "join" and without using "where" clause. Suppose the table is having two columns :...
15
by: uwcssa | last post by:
I try to drop a table as: I got: During SQL processing it returned: SQL0478N The object type "TABLE" cannot be dropped because there is an object "sch.SQL070515104729271", of type "FUNCTION",...
0
by: SMH | last post by:
Hi All, I am currently learning .Net 2, studying for 70-528. I've hit a bit of a brick wall with DataColumn.Expression. As I understand it, this can be used to (For example) concatenate two...
5
by: cart1443 | last post by:
So I know I need to add a few more lines somewhere to make this work, but I'm not sure how. // Lab 3: EmployeeTest.java // Application to test class Employee. /* Begin class declaration of...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.