GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Tech FAQs  >  SQL
Go To First  |  Previous Question  |  Next Question 
 SQL  |  Question 22 of 182    Print  
What is the Querry for retrieving the 2nd highest salary in a table?

  
Total Answers and Comments: 42 Last Update: May 14, 2008     Asked by: suman 
  
 Sponsored Links



 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
  Sorting Options  
  Page 1 of 5   « First    1    2    3    >     Last »  
March 18, 2006 22:23:38   #1  
Culver_lake Member Since: March 2006   Contribution: 46    

RE: What is the Querry for retrieving the 2nd highest ...

SELECT SALARY

FROM    EMPLOYEE

WHERE  SALARY=(SELECT MAX(SALARY)

                         FROM    EMPLOYEE

                         WHERE  SALARY <> (SELECT MAX(SALARY)

                                                      FROM    EMPLOYEE))


 
Is this answer useful? Yes | No
March 20, 2006 00:46:28   #2  
gomathi.e        

RE: What is the Querry for retrieving the 2nd highest ...
select max(salary) from <t.name> where salary!=(select max(salary) from <t.name>)
 
Is this answer useful? Yes | No
March 20, 2006 03:37:29   #3  
muralir02 Member Since: March 2006   Contribution: 1    

RE: What is the Querry for retrieving the 2nd highest ...
select max(sal) from emp where sal<(select max(sal) from emp);
 
Is this answer useful? Yes | No
March 20, 2006 04:55:46   #4  
kumarbp2005 Member Since: February 2006   Contribution: 2    

RE: What is the Querry for retrieving the 2nd highest ...

The Query is:

SELECT MAX(SAL) AS SAL FROM EMPLOYEE WHERE SAL<>(SELECT MAX(SAL) FROM EMPLOYEE)


 
Is this answer useful? Yes | No
March 23, 2006 23:47:45   #5  
satish        

RE: What is the Querry for retrieving the 2nd highest ...

SELECT MAX(E1.salary)  FROM ggg E1 , ggg E2
WHERE E1.salary< E2.salary

2.select top 1 salary  from ggg where salary<(select max(salary) from ggg)
order by  salary  desc

3.name   salary
rahul   15000
ronak   20000
saumil  25000

create table ggg (name varchar(20),salary int)

insert into ggg values ('saumil',25000)

select * from ggg

Select MAX(Salary) from ggg
Select MAX(Salary) from ggg where Salary <> (Select MAX(Salary) from ggg)

select top 1 salary  from ggg where salary<(select max(salary) from ggg)
order by  salary  desc

SELECT MAX(E1.salary)  FROM ggg E1 , ggg E2
WHERE E1.salary< E2.salary


 
Is this answer useful? Yes | No
March 24, 2006 08:40:46   #6  
Culver_lake Member Since: March 2006   Contribution: 46    

RE: What is the Querry for retrieving the 2nd highest ...

doesn't it appear from the volume of code in your solution that it is not optimal. It's clearly like using a sledge hammer to crush a walnut. In the real world no one would bear the overhead of this solution when the others on the page do the same thing without creating tables and certainly without that last join step.

SQL is a non-procedural language, your solution is completely procedural.


 
Is this answer useful? Yes | No
March 29, 2006 02:54:01   #7  
tapas        

RE: What is the Querry for retrieving the 2nd highest ...
select * from (select rownum rn,x.* from(select * from emp)x)where rn=2;
 
Is this answer useful? Yes | No
March 29, 2006 12:45:08   #8  
Culver_lake Member Since: March 2006   Contribution: 46    

RE: What is the Querry for retrieving the 2nd highest ...

Tapas

I don't think that will work because your query is based on the row number and not the value of the salary column. It's the 2nd highest salary, not the row with the 2nd highest rownumber.

In general for a relational database queries involving physical aspects of the table (like rownum) should be avoided.  The relational model is based on comparing values that represent facts about things in the real world, not facts about the internal makeup of tables rows and columns.


 
Is this answer useful? Yes | No
March 29, 2006 16:31:05   #9  
vikramp Member Since: March 2006   Contribution: 3    

RE: What is the Querry for retrieving the 2nd highest ...

This should work

Select Max(Salary)
From tblSalary
Where Salary < (Select Max(Salary) From tblSalary)

Vikram


 
Is this answer useful? Yes | No
March 30, 2006 05:33:10   #10  
srinivas        

RE: What is the Querry for retrieving the 2nd highest ...

Hi  u r telling very good answer.

Thanks

Srinivas


 
Is this answer useful? Yes | No
  Page 1 of 5   « First    1    2    3    >     Last »  


 
Go To Top


 Sponsored Links



 
Sponsored Links

 
Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape