Skip to content
Theory · Selecting and sorting · lesson 6 of 10

LIMIT and OFFSET

Think of a search in an online store. The search found 340 products, but the screen shows only the first 20, with buttons "Page 2", "Page 3" at the bottom. The second page skips the first 20 products and shows the next 20. The store doesn't dump all 340 at once: nobody would read that many.

SQL has two words for this. LIMIT says how many rows to return, and OFFSET says how many rows to skip before them.

LIMIT: how many rows

LIMIT goes at the very end of the query, after ORDER BY, and the number comes right after the word LIMIT. Here are the three priciest menu items:

SQL
SELECT name, price
FROM menu_items
ORDER BY price DESC
LIMIT 3;
TEXT
     name      | price
---------------+--------
 Pumpkin latte | 320.00
 Syrniki       | 310.00
 Cheesecake    | 290.00
(3 rows)

Let's go through the query:

  1. ORDER BY price DESC arranges all 13 items from the priciest to the cheapest.
  2. LIMIT 3 keeps the first three rows of that list and drops the other ten.

Interviewers often ask for such a query: "find the three most expensive products".

Without ORDER BY, any rows

LIMIT takes the first rows of the result in the order they came. Without ORDER BY the DBMS picks that order, and it is not promised. So SELECT name FROM menu_items LIMIT 3 returns some three items, but you can't tell in advance which ones. Tomorrow they may be a different three.

The same trouble awaits with ORDER BY too, if there is a tie at the cut. Say you need the five cheapest items. After ORDER BY price, the fifth and sixth places go to cappuccino and latte, both at 220.00. Which of them makes the top five is not promised. A second column in ORDER BY removes the tie:

SQL
SELECT name, price
FROM menu_items
ORDER BY price, name
LIMIT 5;
TEXT
    name    | price
------------+--------
 Espresso   | 140.00
 Green tea  | 150.00
 Croissant  | 160.00
 Americano  | 170.00
 Cappuccino | 220.00
(5 rows)

Now the fifth is always Cappuccino: alphabetically it comes before Latte.

OFFSET: how many to skip

OFFSET is usually written right after LIMIT. It skips the given number of rows, and only then LIMIT counts its own. That is how you get pages. Here is the menu in alphabetical order, four items per page, page 2:

SQL
SELECT name, price
FROM menu_items
ORDER BY name
LIMIT 4 OFFSET 4;
TEXT
     name      | price
---------------+--------
 Espresso      | 140.00
 Flat white    | 250.00
 Green tea     | 150.00
 Hot chocolate | 240.00
(4 rows)

The first four names in alphabetical order are Americano, Cappuccino, Cheesecake, Croissant. OFFSET 4 skipped them, and LIMIT 4 took the next four.

One rule works for any page: if a page holds K rows, page number N is LIMIT K OFFSET (N - 1) * K. The first page is OFFSET 0, which you can leave out.

Here the store analogy breaks down. The store remembers what it showed on the first page. The DBMS computes each page anew, as a separate query. If without ORDER BY the order changes from run to run, the same item may land on two pages, and another on none.

LIMIT and the SQL standard

PostgreSQL, MySQL and SQLite understand LIMIT and OFFSET, but the SQL standard doesn't have them. The standard form is longer: OFFSET 4 ROWS FETCH FIRST 4 ROWS ONLY. PostgreSQL understands both, and in this course we write the short one.

When LIMIT kicks in

In the first chapter the run order was FROM, WHERE, SELECT, ORDER BY. LIMIT goes at the very end, after ORDER BY. The widget has eight menu items, and the query takes the three priciest. Press "Next" and watch two moments: when the category column disappears and when rows get cut off.

LiveWhen LIMIT kicks inrows: 8
SELECT name, price
1FROM menu_items
ORDER BY price DESC
LIMIT 3;
namecategoryprice
Espressocoffee140.00
Green teatea150.00
Croissantfood160.00
Cappuccinocoffee220.00
Lattecoffee220.00
Rafcoffee280.00
Syrnikifood310.00
Pumpkin lattecoffee320.00

FROM: the database takes table menu_items — 8 rows. Press Next.

The number left of a clause is when it runs

Three rules follow from this order:

  • ORDER BY comes after SELECT, so it sees the aliases from it;
  • LIMIT comes after ORDER BY and cuts rows that are already sorted, so ORDER BY price DESC LIMIT 3 gives exactly the three priciest;
  • without ORDER BY the rows reach this step in no particular order, and LIMIT takes the first ones that come.

Common mistake

LIMIT was written before ORDER BY:

SQL
SELECT name, price
FROM menu_items
LIMIT 3
ORDER BY price DESC; -- ✗
TEXT
ERROR:  syntax error at or near "ORDER"

This is a syntax error: the written order of clauses is strict, and LIMIT and OFFSET always come after ORDER BY. The full query is written in this order: SELECT, FROM, WHERE, ORDER BY, LIMIT, OFFSET.

Key points

  • LIMIT n keeps the first n rows of the result, and OFFSET m first skips m rows.
  • Without ORDER BY, LIMIT returns whatever rows come; a tie at the cut is broken with a second column.
  • Page N of K rows is LIMIT K OFFSET (N - 1) * K.
  • In terms of meaning LIMIT runs last, after SELECT and ORDER BY, and it is written at the end too.