Reputation: 15
My problem sounds simple enough but I havent been able to find a solution that works. I need to get the row with say 5th highest value of an attribute with sqlite.. Entries are random of course. So its not sorted. I did find a few solutions with sql but apparently not all sql functionalities are supported by sqlite.
Thanks in advance
Upvotes: 1
Views: 1303
Reputation: 184
Try the 'Limit' keyword.
The query below will eliminate the first four rows by using 'not in'. Then the last 'Limit 1' will select the 5th row.
Select * from Programs Where ProgramId not in (Select ProgramId From Programs order by programId limit 4 ) order by programId Limit 1
EDIT:
To add in CV's 'Distinct' and 'OFFSET' suggestions, the finished query would look something like...
Select StudentName From Students Where Marks in (
Select distinct Marks from Students Order By Marks desc Limit 1 offset 4)
Upvotes: 0
Reputation: 180010
To remove duplicates, use DISTINCT. To get only the fifth value, use the OFFSET clause:
SELECT DISTINCT SomeAttribute
FROM MyTable
ORDER BY 1 DESC
LIMIT 1 OFFSET 4
Upvotes: 2