Paul Taylor
Paul Taylor

Reputation: 13210

Is there a way to get a range of records in Postgres using LIMIT keyword

I have a table with a count column, I want to get batches of the records in the table base don the value of count. Note the count field is not a unique id - there maybe many rows with the same count.

I can use this query to get the 30000 records with the highest count

select *
from artist t1
order by count desc
LIMIT 30000;

but is there a way to get a range, i.e records 30001 to 60000, records 60001 to 90000 ect.

Upvotes: 2

Views: 838

Answers (1)

Matt
Matt

Reputation: 15071

Use the OFFSET function.

First 30000:

SELECT *
FROM artist t1
ORDER BY count DESC
LIMIT 30000;

30001 to 60000

SELECT *
FROM artist t1
ORDER BY count DESC
LIMIT 30000 OFFSET 30001;

60001 to 90000

SELECT *
FROM artist t1
ORDER BY count DESC
LIMIT 30000 OFFSET 60001;

Upvotes: 2

Related Questions