Contents / Previous / Next


LIMIT and OFFSET

LIMIT allows you to retrieve only a portion of the rows that are generated by the rest of the query. SELECT * FROM table [LIMIT { number | ALL }] [OFFSET number] If a limit count is given, no more than that many rows will be returned.
LIMIT ALL is the same as omitting a LIMIT clause.

With OFFSET you may skip rows at the beginning.
OFFSET 0 is the same as omitting an OFFSET clause.
If both OFFSET and LIMIT appear, then OFFSET rows are skipped before starting to count the LIMIT rows.

> SELECT * FROM stud; id | name | semester | diploma | gender ----+------+----------+---------+----- 1 | fred | 2 | bio | m 3 | tom | 1 | bio | m 4 | john | 3 | phy | m 2 | lisa | 2 | bio | f (4 rows) > SELECT * FROM stud ORDER BY id LIMIT 2 OFFSET 2; id | name | semester | diploma | gender ----+------+----------+---------+----- 3 | tom | 1 | bio | m 4 | john | 3 | phy | m (2 rows) When using LIMIT, it is a good idea to also use an ORDER BY clause that constrains the result rows into a unique order. Otherwise you will get an unpredictable subset of the query's rows.