LIMIT

LIMIT explained with practical SQL patterns, edge cases, and production-ready guidance.

5 min read · Last updated: March 2026 · Back to overview

Quick Answer

The LIMIT clause restricts how many rows a SELECT query returns. Combined with OFFSET, it enables pagination. Always pair LIMIT with ORDER BY to get a predictable, consistent subset of rows.

Spin up a Postgres database in 20 seconds with Vela.

Try Vela Sandbox

Introduction to PostgreSQL LIMIT

LIMIT is an optional clause of the SELECT statement that caps the number of rows returned. It is commonly used for pagination, sampling, and "top N" queries.

Basic syntax:

SELECT select_list
FROM table_name
ORDER BY sort_expression
LIMIT row_count;

If row_count is zero, the query returns an empty set. If row_count is NULL, the LIMIT clause has no effect and all rows are returned.

To skip a number of rows before returning results, add OFFSET:

SELECT select_list
FROM table_name
ORDER BY sort_expression
LIMIT row_count OFFSET row_to_skip;

PostgreSQL evaluates OFFSET before LIMIT: it skips the first row_to_skip rows, then returns the next row_count rows from the remaining set.

LIMIT examples

These examples use the film table from the DVD rental sample database.

Get the first 5 films by film ID:

SELECT film_id, title, release_year
FROM film
ORDER BY film_id
LIMIT 5;

Result:

film_id |      title       | release_year
---------+------------------+--------------
      1 | Academy Dinosaur |         2006
      2 | Ace Goldfinger   |         2006
      3 | Adaptation Holes |         2006
      4 | Affair Prejudice |         2006
      5 | African Egg      |         2006
(5 rows)

Get 4 films starting from position 4 (LIMIT with OFFSET):

SELECT film_id, title, release_year
FROM film
ORDER BY film_id
LIMIT 4 OFFSET 3;

Result:

film_id |      title       | release_year
---------+------------------+--------------
      4 | Affair Prejudice |         2006
      5 | African Egg      |         2006
      6 | Agent Truman     |         2006
      7 | Airplane Sierra  |         2006
(4 rows)

The OFFSET 3 skips the first three rows, then LIMIT 4 returns the next four.

Top 10 most expensive rentals:

SELECT film_id, title, rental_rate
FROM film
ORDER BY rental_rate DESC
LIMIT 10;

Result:

film_id |        title        | rental_rate
---------+---------------------+-------------
     13 | Ali Forever         |        4.99
     20 | Amelie Hellfighters |        4.99
      7 | Airplane Sierra     |        4.99
     10 | Aladdin Calendar    |        4.99
      2 | Ace Goldfinger      |        4.99
...

Why ORDER BY is required with LIMIT

PostgreSQL stores rows internally in an unspecified order. Without ORDER BY, the rows returned by LIMIT may differ between queries or after table updates. Always include ORDER BY to ensure you consistently get the rows you intend:

-- Correct: deterministic result
SELECT film_id, title FROM film ORDER BY film_id LIMIT 10;

-- Risky: unpredictable which 10 rows are returned
SELECT film_id, title FROM film LIMIT 10;

LIMIT / OFFSET pagination and its limitations

LIMIT and OFFSET are widely used for page-based pagination (page 1 = OFFSET 0, page 2 = OFFSET 20, etc.). However, at large offsets this becomes slow because PostgreSQL must scan and discard all preceding rows before returning the page you want.

For large tables or high page numbers, consider keyset (cursor-based) pagination instead:

-- Page 1
SELECT film_id, title FROM film ORDER BY film_id LIMIT 20;

-- Next page: use the last film_id from the previous result
SELECT film_id, title FROM film WHERE film_id > 20 ORDER BY film_id LIMIT 20;

Keyset pagination is O(1) per page instead of O(offset), making it much faster for deep pages.

Tips for using LIMIT effectively

  • Always pair LIMIT with ORDER BY to get consistent, predictable results.
  • For "top N" queries, an index on the sort column allows PostgreSQL to use an efficient index scan and stop early once N rows are found.
  • Avoid large OFFSET values on big tables — performance degrades linearly. Switch to keyset pagination for high-volume use cases.
  • LIMIT 1 is useful when you need exactly one row (e.g., the latest record), but remember it only makes sense with an explicit ORDER BY.

Reference: PostgreSQL documentation — SELECT / LIMIT.

Continue in Filtering Data: FETCH.

Related in this section: PostgreSQL WHERE · AND Operator · OR Operator

Frequently Asked Questions

What happens if LIMIT is larger than the number of rows in the table?

PostgreSQL simply returns all available rows without error. If the table has 50 rows and you run LIMIT 1000, you get 50 rows back. LIMIT is a maximum cap, not a required count.

What is the difference between LIMIT and FETCH in PostgreSQL?

They are functionally equivalent for simple use cases. FETCH FIRST n ROWS ONLY is the SQL standard syntax, while LIMIT is a PostgreSQL/MySQL extension. Both accept OFFSET (or OFFSET before FETCH). Use FETCH if you need standard SQL compatibility; LIMIT is more concise and widely used in practice.

Why is LIMIT with a large OFFSET slow?

PostgreSQL must scan through all the rows up to the OFFSET point before returning results. So LIMIT 20 OFFSET 10000 forces PostgreSQL to process 10,020 rows and discard the first 10,000. Use keyset pagination (WHERE id > last_seen_id ORDER BY id LIMIT 20) for efficient deep pagination.

Can I use LIMIT without ORDER BY?

Yes, but the result is non-deterministic — PostgreSQL may return different rows on different executions or after a table rewrite (e.g., VACUUM FULL). Always use ORDER BY with LIMIT if you need consistent, reproducible results.

Does LIMIT affect query performance if the table is large?

It can significantly improve performance. When LIMIT is combined with ORDER BY on an indexed column, PostgreSQL can use an index scan and stop after finding the required number of rows, avoiding a full table scan. Without an index, PostgreSQL may still need to sort the entire result set before applying LIMIT.