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
4 7498
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
-
select top 1 * from
-
(select top 4 * FROM empl
-
order by salary desc) e
-
order by salary
-
This is for 4th highest salary. you can replace 4 with any number you want by the number for that much highest salary
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
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: -
SELECT *
-
FROM
-
( select DENSE_RANK() OVER (ORDER BY Salary DESC) AS Rank, * from empl ) AS E1
-
WHERE [Rank] IN (3,4,5)
-
DENSE_RANK() is the gives ranking for the rows
thanks
Approach 1 using DENSE_RANK()
Consider following details
table: Emp
Column: name, Salary - (SELECT name , salary , DENSE_RANK() over (order by salary desc) AS Rowno FROM emp) a
-
Where a.Rowno=3
Above example is for getting third largest salary.
Appraoch 2: using subquery - SELECT MIN(Salary) from EMP where salary in
-
(SELECT TOP 3 salary from emp e order by salary desc)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 '<'...
|
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 _
|
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...
|
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.
|
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)...
| |
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.
|
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
|
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:
------------
|
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
{
|
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...
|
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...
| |
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. ...
|
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...
|
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...
|
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...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |