Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select only 1 row after ordering

Is it possible to get only 1 row after ordering? I don’t want to load DB so much, so i want to get only 1 row (it may be from middle), here is example:

ID User Points
1 user1 10
2 user2 60
3 user3 45

I want to get the fe. second user after ORDER BY `points` DESC, so the table will look like

ID User Points
2 user2 60
3 user3 45
1 user1 10

The second user is user3, and i want to return just that one row. It is possible? Or do I need to get all and just LIMIT it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you want to strictly get only one row, corresponding to the second highest points, then my guess is that on ties you want the lowest id. In that case you can use the LIMIT function accordingly.

SELECT * 
FROM tab
ORDER BY points DESC, 
         ID
LIMIT 1,1

The so written LIMIT clause will allow you to start limiting from the second row, then takes the first row available (which is the second one).

Check the demo here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading