Updated on 2025-10-23 GMT+08:00

Pagination Queries

The syntax is as follows:

SELECT query_list FROM table_name [ LIMIT { [offset,] count | ALL } ]
  • offset: indicates the number of rows to skip before starting to return rows.
  • count: indicates the number of data records to be retrieved after the skipped rows.
  • ALL: to retrieve all data after the skipped rows.
-- Create a table and insert 100 data records into the table.
m_db=# CREATE TABLE testl(id int PRIMARY KEY, flag varchar(10));
m_db=# INSERT INTO testl (id, flag) VALUES (generate_series(1,100),generate_series(1,100));

-- Query the first five data records.
m_db=# SELECT * FROM testl ORDER BY 1  LIMIT 5;
 id | flag  
----+-------
  1 | 1
  2 | 2
  3 | 3
  4 | 4
  5 | 5
(5 rows)

-- Query four data records after the twentieth record.
m_db=# SELECT * FROM testl ORDER BY 1 LIMIT 20,4;
 id |  flag  
----+--------
 21 | 21
 22 | 22
 23 | 23
 24 | 24
(4 rows)

-- Query all data after the ninety-sixth record.
m_db=# SELECT * FROM testl ORDER BY 1 LIMIT 96,ALL;
 id  |  flag   
-----+---------
  97 | 97
  98 | 98
  99 | 99
 100 | 100
(4 rows)

-- Drop.
m_db=# DROP TABLE testl;