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
4 7487
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
-
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
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: -
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...
|
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...
|
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...
|
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%...
|
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 :...
|
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",...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
|
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...
| | |