LIMIT
Caps the number of rows a SELECT returns. OFFSET skips rows first. FETCH FIRST is the standard spelling of LIMIT.
Syntax
SELECT ...
FROM <source> [ <alias> ]
[ WHERE ... ]
[ ORDER BY ... ]
{ LIMIT { <count> | ALL } [ OFFSET <skip> [ ROW | ROWS ] ]
| OFFSET <skip> [ ROW | ROWS ] [ LIMIT <count> ]
| [ OFFSET <skip> [ ROW | ROWS ] ] FETCH { FIRST | NEXT } [ <count> ] { ROW | ROWS } ONLY }
Parameters
| Parameter | Description |
|---|---|
<count> | How many rows to return. An integer literal. |
ALL | No cap. Same as leaving LIMIT out. |
<skip> | How many rows to skip before counting. An integer literal. |
FETCH FIRST <count> ROWS ONLY | Same as LIMIT <count>. Without <count> it returns one row. |
Usage notes
LIMITandOFFSETmay come in either order.LIMIT 0is read as no cap, likeLIMIT ALL. It does not return zero rows.- Without
ORDER BYthe rows kept are not defined. PairLIMITwithORDER BYfor a stable top-n or page. LIMITapplies afterDISTINCTandORDER BY.- After a set operation,
LIMITbelongs to the last branch only. To cap the whole result, wrap the set operation in a derived table. See Set operators. - The
querytool returns at most 1000 rows whatever theLIMIT, and says when it cut the result. Useexportfor more. - In the body of
CREATE TOOL,<count>and<skip>may be parameters:LIMIT :top.
Examples
The ten largest orders:
SELECT o.key, o.amount FROM Order o ORDER BY o.amount DESC LIMIT 10
The third page of twenty:
SELECT o.key, o.placed_at FROM Order o ORDER BY o.placed_at DESC, o.key LIMIT 20 OFFSET 40
The standard spelling:
SELECT o.key FROM Order o ORDER BY o.amount DESC OFFSET 5 ROWS FETCH FIRST 5 ROWS ONLY
Not supported
| Refused | Write instead |
|---|---|
A LIMIT or OFFSET that is not an integer literal | LIMIT <n> with a number |
FETCH FIRST ... WITH TIES | LIMIT <n>, or RANK() in a derived table and keep rank <= n |
FOR UPDATE, FOR SHARE | Leave it out: queries are read-only |
-- Refused by the engine
SELECT o.key FROM Order o LIMIT o.amount
-- Refused by the engine
SELECT o.key FROM Order o ORDER BY o.amount FETCH FIRST 5 ROWS WITH TIES