Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have tried so many MYSQL queries from many sites, but i cant get my required result.Can anyone please help me out on this issue?
Posted

You can try
SQL
SELECT * FROM table_name ORDER BY desired_column DESC LIMIT 2;

This query will give you 2 rows in return where the second row is the one you want.
(I use SELECT * as I know nothing about your table structure)

I hope this helps.
 
Share this answer
 
v2
Comments
Janardhanam Julapalli 28-Oct-14 4:46am    
@George Jonsson-This is working fine.
George Jonsson 28-Oct-14 4:52am    
Thanks, but see solution 3 for a more elegant code.
Maciej Los 28-Oct-14 12:20pm    
+5!
So read the official documentation[^].

The simplest query would probably be:
SQL
SELECT  *
FROM    EMPLOYEES e
ORDER BY e.SALARY DESC
LIMIT 1,1;
 
Share this answer
 
Comments
Janardhanam Julapalli 28-Oct-14 4:49am    
Very Impressive dude@Jorgen Andersson
George Jonsson 28-Oct-14 4:51am    
One learns something new everyday.
Pikoh 28-Oct-14 4:54am    
Fived. Simple and elegant.
Maciej Los 28-Oct-14 12:19pm    
+5!
Shweta N Mishra 29-Oct-14 8:25am    
I guess "LIMIT" works in MySQL, How about SQL server do we have any replace for it?
Try something like this
SQL
SELECT * FROM
(SELECT *
FROM Workers
ORDER BY salary DESC
LIMIT 2) alias
ORDER BY salary
LIMIT 1


I've got no mysql at hand to try it, so i hope it works
 
Share this answer
 
v2
Comments
Janardhanam Julapalli 28-Oct-14 4:48am    
@pikoh-This is working, but there is a small alias error.This is my updated code
SELECT * FROM
(SELECT *
FROM user_details
ORDER BY salary DESC
LIMIT 2)
user_details ORDER BY salary
LIMIT 1
Pikoh 28-Oct-14 4:53am    
You are right. I have updated solution :)
Janardhanam Julapalli 28-Oct-14 5:03am    
Thank u all for ur replies.I am seeing this forum as very useful one.

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