CoolInterview.com - World's Largest Collection of Interview Questions & Answers, FAQs, queries, sample papers, exam papers, dumps, what, why, how, where, when questions
Interview Success Kit - Get Success in Job Interviews Interview Success Kit - 30,000 Interview Que. & Ans.
Covers 30,000+ Interview Questions asked in actual MNC Interviews - all with model answers.
Get Your Dream Job Now - www.vyoms.com
Sponsored Links
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 (3.0) By 31 users
Added on 7/25/2011
Views 2717
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

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 & Answers

    User Options
    Latest 20 Questions
    How to create arrays in JavaScript?
    How can JavaScript codes be hidden from old browsers that don?t support JavaScript ?
    What is namespacing in JavaScript and how is it used?
    What are the decodeURI() and encodeURI()?
    How are DOM utilized in JavaScript?
    Write the point of difference between web-garden and a web-farm?
    What is the ?Strict? mode in JavaScript and how can it be enabled ?
    What are escape characters?
    How can convert the string of any base to integer in JavaScript?
    What is the difference between ViewState and SessionState?
    Write the code for adding new elements dynamically ?
    Between JavaScript and an ASP script, which is faster?
    What are JavaScript types ?
    What qualities do you look for in a boss?
    How you would be an asset to this company?
    Why should we hire you?
    What is project implementation?
    Differentiate Public and Private Accounting?
    Explain what is compound journal entry ?
    Differentiate between provision and reserve?


    Copyright ©2003-2016 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 Seconds