473,545 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

95 New Member
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',10 0)
insert into empl values(2,'B',20 0)
insert into empl values(3,'C',30 0)
insert into empl values(4,'D',40 0)
insert into empl values(5,'E',50 0)
insert into empl values(6,'F',60 0)

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 7498
siva538
44 New Member
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',10 0)
insert into empl values(2,'B',20 0)
insert into empl values(3,'C',30 0)
insert into empl values(4,'D',40 0)
insert into empl values(5,'E',50 0)
insert into empl values(6,'F',60 0)

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
ganeshkumar08
31 New Member
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 Recognized Expert New Member
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',10 0)
insert into empl values(2,'B',20 0)
insert into empl values(3,'C',30 0)
insert into empl values(4,'D',40 0)
insert into empl values(5,'E',50 0)
insert into empl values(6,'F',60 0)

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
deveshakgec
5 New Member
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
2482
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 the '<' for the insert() and '==' operator for find() however it doesn't seem to be the case In fact it seems to only need to overload the '<'...
5
8264
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
11506
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 receive a 6% raise, one rated good will receive a 4% raise, and one rated poor will receive a 1.5% raise. Add the if... else... statements to program...
22
13983
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 salary.
5
8557
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% of the gross salary. The income tax is computed progressively after detracting NI and pension contributions. The taxable amount (after detractions)...
2
3447
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 : Employee_id, Employee_salary. Please let me know.. I faced this question in an interview and I did not have any answer for this.
15
10777
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", which depends on it. SQLSTATE=42893 Then I tried to do
0
4515
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 columns to make a derived column. When I run my code, I get this error: ------------
5
3859
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 EmployeeTest class. */ public class EmployeeTest {
0
7464
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7396
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7751
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5323
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1874
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.