Find NTH Maximum Value in SQL Server
Find NTH Maximum Value in SQL Server
This aritlce is written by Hariharan Velayuthan. He writes "There are several methods to find out the
Nth maximum/minimum value using SQL. This article discusses on such method to find Nth
maximum value from a desired table. This article is aimed at users who are in the beginner or
intermediate level in SQL Server."
[Note: This article assumes that the reader is familiar with the T-SQL, joins and queries]
I have taken utmost effort to make this article easy to understand, but incase you are not clear with
the concept, please raise up your concern and Ill be more than happy to attend your doubts. All the
examples discussed in this article uses Employee table. If you do not have this table, please use the
following script to create it.
Use Pubs
Go
Create table Employee
(
Eid int,
Name varchar(10),
Salary money
)
Go
Insert into Employee values (1,'harry',3500)
Insert into Employee values (2,'jack',2500)
Insert into Employee values (3,'john',2500)
Insert into Employee values (4,'xavier',5500)
Insert into Employee values (5,'steven',7500)
Insert into Employee values (6,'susana',2400)
Go
A simple query that can find the employee with the maximum salary, would be:
Returns:
Eid
Name
Salary
steven
7500
If the same syntax is applied to find out the 2nd or 3rd or 4th level of salary, the query would
become bit complex to understand. See the example below:
The above query would go on and on, depending on the level of salary that is to be determined. As
mentioned earlier, the SQL Engine evaluates the inner most query first and moves the next outer
level. One wouldnt want to write such a big query just to find out this simple information.
The same result can be achieved with a simple syntax and easily understandable logic, by using a
CORRELATED SUBQUERY. This article doesnt explain about correlated sub-query as it is out of scope
of this article. (You may want to take a quick look on CORRELATED SUBQUERY.) As a "Rule of
Thumb" keep these points in mind, when you use a correlated sub-query
1. Correlated sub-query is a performance overhead to the database server and so, you have to
use it only if it is required
2. Avoid using Correlated subquery on large tables, as the inner query is evaluated for each
row of the outer query
Having said that, lets look at the query that captures the Nth maximum value:
Since the outer querys value is referred in the inner query, the operation is done row-by-row. Based
on the sample data as shown above, the process starts with the following record:
Employee E1
---------------------------------Eid
Name
Salary
harry
3500
The salary of this record is substituted in the inner query and evaluated as:
Above query returns 2 (as there are only 2 salaries greater than 3500). This value is substituted in
the outer query and will be evaluated as:
The "where" condition evaluates to FALSE and so, this record is NOT fetched in the result.
Next the SQL Engine processes the 2nd record which is:
Employee E1
---------------------------------Eid
Name
Salary
jack
2500
This query returns a value of 3 (as there are 3 salaries greater than 2500). The value is substituted
in the outer query and evaluated as:
The "where" condition evaluates to TRUE and so, this record IS fetched in the result. This operation
continues for all the remaining records. Finally the result shows these 2 records:
Eid
Name
Salary
jack
2500
john
2500
The above query works in the same manner in Oracle and Sybase as well. Applying the same logic,
to find out the first maximum salary the query would be:
If you are able to understand this functionality, you can workout various other queries in the same
manner. The bottom line is, the query should be efficient and NOT resource hungry.
into
into
into
into
into
into
Employee
Employee
Employee
Employee
Employee
Employee
values
values
values
values
values
values
(1,'harry',3500)
(2,'jack',2500)
(3,'john',2500)
(4,'xavier',5500)
(5,'steven',7500)
(6,'susana',2400)
Correlated subqueries
In the subqueries previously discussed, SQL evaluates the subquery once, substitutes the result of
the subquery in the search condition, and evaluates the outer-level SELECT based on the value
of the search condition. You can also write a subquery that SQL may need to re-evaluate as it
examines each new row (WHERE clause) or group of rows (HAVING clause) in the outer-level
SELECT. This is called a correlated subquery.
http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?
topic=/sqlp/rbafycorrs.htm
Reference Link
http://www.sqlteam.com/article/find-nth-maximum-value-in-sql-server
http://mssqlserver.wordpress.com/2006/11/14/how-do-you-find-thesecond-highest-salary/
http://hemantg.blogspot.com/2007/06/retrieving-nth-salary-in-sqlserver.html