Using limit and offset
The limit
clause is the PostgreSQL way to limit the number of rows returned by a query, whereas the offset
clause is used to skip a specific number of rows returned by the query.
limit
and offset
are used to return a portion of data from a resultset generated by a query; the limit
clause is used to limit the number of records in output and the offset
clause is used to provide PostgreSQL with the position in the resultset from which to start returning data.
They can be used independently or together.
Now let’s test limit
and offset
using the following queries:
forumdb=> select * from categories order by pk limit 1;
pk | title | description
----+----------+------------------------------
1 | Database | Database related discussions
(1 row)
The preceding query returns only the first record that we have inserted; this is because the pk
field is an integer type with a default value generated always as the identity.