CoolInterview.com - World's Largest Collection of Interview Questions
Start Your Own website Now
Sponsored Links

Interview Questions
Our Services

Get 9,000 Interview Questions & Answers in an eBook.


  • 9500+ Pages
  • 9000 Question & Answers
  • All Tech. Categories
  • 14 MB Content

    Get it now !!



    Send your Resume to 6000 Companies


  • 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 (0.4) By 1068 users
    Added on 7/25/2006
    Views 4111
    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

    Amit Shah's answer works if there is only one record for top 3 salaries.

    Including distinct command will work in all cases. Hence the query becomes,

    SELECT * FROM `table_name` WHERE field_salary = (SELECT DISTINCT field_salary FROM `table_name` ORDER BY field_salary DESC LIMIT 4,1)



    Posted by: Sriram Seshadri    

    Contact Sriram Seshadri Contact Sriram Seshadri

    For the query given by Kishor we need to decrement the row number by one as i guess the index starts from 0. So the changed query would be

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

    -Shrek



    Posted by: Shrek    

    Contact Shrek Contact Shrek

    SELECT 4TH HIGHEST SALARY FROM "SALARY" TABLE WITHOUT USING LIMIT.

    YOU CAN REPLACE 4 AS PER YOUR NEEDS.

    select salary from salary s where
    4 =(select count(distinct salary) from salary where s.salary<=salary)



    Posted by: Bade lal verma    

    Contact Bade lal verma Contact Bade lal verma

    SELECT 4TH HIGHEST SALARY FROM "SALARY" TABLE WITHOUT USING LIMIT.

    YOU CAN REPLACE 4 AS PER YOUR NEEDS.

    select salary from salary s where
    4 =(select count(distinct salary) from salary where s.salary<=salary)



    Posted by: Bade lal verma    

    Contact Bade lal verma Contact Bade lal verma

    select salary from employee em where(@n-1)=(select count(*)from employee where salary>em.salary)



    Posted by: supriya chakraborty    

    Contact supriya chakraborty Contact supriya chakraborty

    If you have the better answer, then send it to us. We will display your answer after the approval.
    Rules to Post Answers in CoolInterview.com:-

  • There should not be any Spelling Mistakes.
  • There should not be any Gramatical Errors.
  • Answers must not contain any bad words.
  • Answers should not be the repeat of same answer, already approved.
  • Answer should be complete in itself.
  • 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. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.

    View ALL MYSQL Interview Questions

    User Options
    Sponsored Links


    Copyright ©2003-2014 CoolInterview.com, All Rights Reserved.
    Privacy Policy | Terms and Conditions

    Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Dedicated Servers | C Interview Questions & Answers

    Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes


    Cache = 0.03125 Seconds