Friday, August 20, 2021

How to fetch first n rows only in PostgreSQL

The equivalent of oracles "fetch first n rows only" for postgreSQL is:
select *
from myschema.mytable
limit 10;
If you want to select the coming next 20 rows, but skip the rows you already selected first time, use the keyword OFFSET:
select *
from myschema.mytable
limit 20 offset 10;
The PostgreSQL 11 documentation for this feature can be found here

1 comment:

  1. The exact equivalent of "fetch first N rows only" with PostgreSQL is........."fetch first N rows only"
    I don't know when Oracle got rid of its damned rownum thing but standard SQL syntax has been available for ages with postgres...cf https://www.postgresql.org/docs/8.4/sql-select.html

    ReplyDelete