2

I want to select the last three rows of a table in ascending order. What is the query for that?

1
  • How do we determine which rows are "last" in the table; i.e. what column or set of columns defines the sequence of rows in the table? Apr 17, 2016 at 5:15

2 Answers 2

3

SELECT * FROM table ORDER BY DESC LIMIT 3

3
  • Logically the last three rows for a column in ASC order would be the first three in DESC order. +1 kgb Jul 4, 2010 at 15:27
  • this way you are going to get the right rows, 3 rows is not much to sort not using mysql Jul 4, 2010 at 15:29
  • 1
    then, "SELECT * FROM table ORDER BY ASC LIMIT (FOUND_ROWS() - 3), 3" should work for you(though i didn't test it ;) ) Jul 4, 2010 at 15:36
0
SELECT * 
FROM ( 
        SELECT * 
        FROM tblname
        ORDER BY rowid DESC
     ) WHERE rownum < 4; 

Rowid is the physical address of every row.
Rownum is the psuedo column which is used to generate sequence number of rows.

1
  • Assuming that `rowid` and `rownum` are columns in `tblname`, and there is some kind of relationship between the columns, this query might return the specified result, but unlikely it would be in the specified order. (`rowid` and `rownum` are pseudo-columns available in Oracle, but which are not available in MySQL.) Apr 17, 2016 at 5:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.