-1

How do I select the nth last row from a table in sqlite3 database (Python)?

(with an Id Column in ascending order)

Alexander
  • 73
  • 2
  • 2
  • 5
  • I forgot to mention that it can only be the nth last row and nothing else – Alexander Dec 08 '20 at 12:52
  • Does this answer your question? [How to select the nth row in a SQL database table?](https://stackoverflow.com/questions/16568/how-to-select-the-nth-row-in-a-sql-database-table) – astentx Dec 08 '20 at 12:54

1 Answers1

0

There is no such thing as the nth last row in a table. In SQL tables represent unordered sets.

If you have a column that specifies the ordering, you can use:

select t.*
from t
order by <ordering column> desc
limit 1 offset n - 1;

In SQLite, tables are often created with rowid as a column that is often used to represent the insert order (although that might not always be true). That may be what you want to use as the ordering column.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786