Exforsys

Online Training

How to find second max sal of employee with the co

This is a discussion on How to find second max sal of employee with the co within the Oracle Tutorials forums, part of the Articles and Tutorials category; Hi all I want to display the entire row of the employee who is havong second highest sal can any ...


Go Back   Exforsys > Articles and Tutorials > Oracle Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-20-2004, 12:14 PM
Junior Member
 
Join Date: Sep 2004
Posts: 5
chandraismoon
How to find second max sal of employee with the co

Hi all
I want to display the entire row of the employee who is havong second highest sal

can any one help me

Vaibhav
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 09-20-2004, 09:00 PM
Senior Member
 
Join Date: Nov 2004
Posts: 175
sanereddy is an unknown quantity at this point
Re:How to find second max sal of employee with the co

chandraismoon, Please find the sample code and description on how it works. Let me if you have any questions. 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:

Select * from Employee where salary = (Select max(Salary) from Employee)
How does this query work?

The SQL Engine evaluates the inner most query and then moves to the next level (outer query). So, in the above example inner query i.e. Select max(Salary) from Employee is evaluated first. This query will return a value of 7500 (based on the sample data shown as above). This value is substituted in the outer query and it is evaluated as:

Select * from Employee where salary = (7500)
Returns:

Eid Name Salary
5 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:

Select * from Employee where salary =
(Select max(Salary) from Employee where salary
< (Select max(Salary) from Employee where
Salary < (Select max(Salary) from Employee where
Salary <…………………………………………… N
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 wouldn’t 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 doesn’t 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

Correlated sub-query is a performance overhead to the database server and so, you have to use it only if it is required
Avoid using Correlated subquery on large tables, as the inner query is evaluated for each row of the outer query
Having said that, let’s look at the query that captures the Nth maximum value:

Select * From Employee E1 Where
(N-1) = (Select Count(Distinct(E2.Salary)) From Employee E2 Where
E2.Salary > E1.Salary)
(Where N is the level of Salary to be determined)

In the above example, the inner query uses a value of the outer query in its filter condition meaning; the inner query cannot be evaluated before evaluating the outer query. So each row in the outer query is evaluated first and the inner query is run for that row. Let’s look into the background process of this query, by substituting a value for N i.e. 4,(Idea is to find the 4th maximum salary):

Select * From Employee E1 Where
(4-1) = (Select Count(Distinct(E2.Salary)) From Employee E2 Where
E2.Salary > E1.Salary)
Since the outer query’s 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
1 harry 3500
The salary of this record is substituted in the inner query and evaluated as:

Select Count(Distinct(E2.Salary)) From Employee E2
Where E2.Salary > 3500
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:

Select * From Employee E1 Where (4-1) = (2)
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
2 jack 2500
Now the inner query is evaluated as:

Select Count(Distinct(E2.Salary)) From Employee E2
Where E2.Salary > 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:

Select * From Employee E1 Where (4-1) = (3)
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
2 jack 2500
3 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:

Select * From Employee E1 Where
(1-1) = (Select Count(Distinct(E2.Salary)) From Employee E2 Where
E2.Salary > E1.Salary)

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-28-2007, 12:31 AM
Junior Member
 
Join Date: Aug 2004
Posts: 1
tejinder
select * from emp
where sal in ( select min(sal) from
(select sal from emp
order by sal desc)
where rownum <=2 )

Try this and reply.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-31-2007, 01:41 AM
Junior Member
 
Join Date: Sep 2007
Posts: 2
karthigayini is on a distinguished road
use a simple logic
1st find the max sal of entire table
2nd list all sal less than max sal
3rd from that list find the max

select * from table where salary>(select salary from table where salary <max(salary))
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-09-2008, 02:42 PM
tprabhuit's Avatar
Junior Member
 
Join Date: Dec 2007
Location: Coimbatore
Posts: 16
tprabhuit is on a distinguished road
Send a message via Yahoo to tprabhuit
Hi,

Below is a simple query to find the Nth Maximum / Minimum value of particular column.

Nth Minimum
SELECT * FROM <Table_Name> A
WHERE N = (SELECT COUNT(DISTINCT B.Column_Name ) FROM <Table_Name> B WHERE A.Column_Name >= B.Column_Name)


Where,
N is integer
A & B are alias for the Table

Regards,
Prabhu.T
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 01-26-2008, 05:49 AM
Junior Member
 
Join Date: Dec 2007
Posts: 1
rkm2006 is on a distinguished road
How To Access Oracle Application Forms

HI ALL,
IAM UNABLE TO ACCESS ORACLE APPLICATION FORMS CAN ANYBODY HELPME?

THANKS AND REGARDS
RANJAN KUMAR
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-04-2008, 09:13 AM
Junior Member
 
Join Date: May 2008
Posts: 5
gouthami1970 is on a distinguished road
Perform TOP N ANALYSIS

HI vaibhav Top n analysis is used for knowing the top most ranks.The syntax goes like this modify acc to ur reqirement.

select Rownum as rank,E.* from (select * from emp order by sal desc) where rownum<=2;

Gouthami



Quote:
Originally Posted by chandraismoon View Post
Hi all
I want to display the entire row of the employee who is havong second highest sal

can any one help me

Vaibhav
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 05-16-2008, 03:08 AM
Junior Member
 
Join Date: May 2008
Posts: 1
prajapati is on a distinguished road
Send a message via AIM to prajapati Send a message via Skype™ to prajapati
For OpenSource Database MySQL

If We are Using MySQL the we can use Limit as :

SELECT <col name> FROM <table> order by <col name> DESC Limit (N-1), 1

Where,
N is integer eg. 3rd Max, 4th Max


Regards,
Pradeep Kumar Prajapati
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new questions
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
Apple II Csa2 FAQs: 2Main-Contents, Part 2/25 rubywand@swbell.net Tech FAQ 0 10-07-2004 12:42 AM
FAQ: How to find people's E-mail addresses David Alex Lamb Tech FAQ 0 09-19-2004 05:30 AM
comp.sys.hp.hpux FAQ Ian P. Springer Tech FAQ 0 09-01-2004 12:25 AM
[FAQ] FileMaker Pro - database for Macintosh and Windows Martin Trautmann Tech FAQ 0 04-17-2004 07:26 AM


All times are GMT -4. The time now is 07:47 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.