CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS DATABASE MYSQL DETAILS
Question :
How to display nth highest record in a table for example?How to display 4th highest (salary) record from customer table?


Category MYSQL Interview Questions
Rating (2.9) By 16 users
Added on 7/25/2006
Views 2634
Rate it!
Answers:

Query: SELECT sal FROM `emp` order by sal desc limit (n-1),1If the question: "how to display 4th highest (salary) record from customer table."The query will SELECT sal FROM `emp` order by sal desc limit 3,1



select sal from emp order by descending where rownum=4



 Posted by: Nibedita Swain    

Contact Nibedita Swain  Contact Nibedita Swain

There are two ways
- Using LIMIT clause
- Special SQL

Using LIMIT there are issues that if table has got same salary (field) value multiple times, but this will show you only one record, then you will not get to know that there are also others with same salary.

Another SQL Approach is -
-------------
select a.empid, a.salary from employees as a
where 2 =(select count(distinct b.salary) from employees as b
where b.salary >=a.salary)
---------------
this query will return you all the rows which comes under this criteria.



 Posted by: kishor    

Contact kishor  Contact kishor

select * from TABLENAME where salary=(select salary from TABLENAME order by salary desc limit 4,1)


The following example is for the 6th highest :-
select * from TABLENAME where salary=(select salary from TABLENAME order by salary desc limit 6,1)



 Posted by: Amit Shah    

Contact Amit Shah  Contact Amit Shah

select min(sal) from(select top(4)sal from tbsalary order by sal desc)as tbl



 Posted by: Latika    

Contact Latika  Contact Latika


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL MYSQL Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/7761/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

1.34