Click here to Skip to main content
15,911,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a table with 20 records in it, i want to select the top 5th highest salary in it not top 5 rows or 5th record.

What I have tried:

i have tried some quires but i didn't got the desired results so please help me thank you in advance.
Posted
Updated 4-Oct-16 18:08pm
Comments
[no name] 4-Oct-16 18:32pm    
"i have tried some quires", really? Funny, there doesn't seem to be any SQL queries at all anywhere in your posting indicating that you have actually tried anything.
Suvendu Shekhar Giri 4-Oct-16 21:26pm    
What about a Google search?

If you want to find the top 5 tallest guys in a class, how do you do it?
1. First, sort them up by lining them from the tallest to the shortest (or vice versa);
2. Next, start to count and select the first 5 guys in front.

Do the same in SQL,
A. Use ORDER BY[^] to sort the table by the field of interest, e.g. salary.
B. Next, find out how to get the top 5 rows by checking out SQL SELECT TOP, LIMIT, ROWNUM[^], there are different versions.
Putting A and B in one SQL statement and that will be the answer you are looking for.
 
Share this answer
 
v3
in MySQL
SQL
> SELECT * FROM table ORDER BY hits DESC limit 5;

in Oralce
SQL
> SELECT * FROM table ORDER BY hits DESC where rownum <5;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900