SQLite has a RANK() function
SELECT
*,
RANK () OVER (
ORDER BY age DESC
) age_rank
FROM
yourtable
This would return all the entries with a new 3rd row denoting their age rank
in my testing, this also orders the results by the specified order in RANK()
keep in mind that any WHERE clauses you add will affect the ranking, in order to get the rank of one element, you need some nested statements
SELECT
*
FROM (
SELECT
*,
RANK () OVER (
ORDER BY age DESC
) age_rank
FROM
yourtable
)
WHERE
name = "Zek";
You can also get the n
th oldest by doing nested statements
SELECT
*
FROM (
SELECT
*,
RANK () OVER (
ORDER BY age DESC
) age_rank
FROM
yourtable
)
WHERE
age_rank = 2;
One thing that might matter that i found in my testing is that all values that are the same have the same rank. i personally think this is good but it might matter
RANK() OVER (
PARTITION BY <expression1>[{,<expression2>...}]
ORDER BY <expression1> [ASC|DESC], [{,<expression1>...}]
)
pretty good tutorial i found
reference to it in official docs though does not explain how it works
of course the """fun""" of sqlite is that every implementation is different, though its in the official docs so i cant imagine it would be that rare