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:
SELECT name, price
FROM menu_items
ORDER BY price DESC
LIMIT 3; name | price
---------------+--------
Pumpkin latte | 320.00
Syrniki | 310.00
Cheesecake | 290.00
(3 rows)Let's go through the query:
ORDER BY price DESCarranges all 13 items from the priciest to the cheapest.LIMIT 3keeps 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:
SELECT name, price
FROM menu_items
ORDER BY price, name
LIMIT 5; 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:
SELECT name, price
FROM menu_items
ORDER BY name
LIMIT 4 OFFSET 4; 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.
| name | category | price |
|---|---|---|
| Espresso | coffee | 140.00 |
| Green tea | tea | 150.00 |
| Croissant | food | 160.00 |
| Cappuccino | coffee | 220.00 |
| Latte | coffee | 220.00 |
| Raf | coffee | 280.00 |
| Syrniki | food | 310.00 |
| Pumpkin latte | coffee | 320.00 |
FROM: the database takes table menu_items — 8 rows. Press Next.
Three rules follow from this order:
ORDER BYcomes afterSELECT, so it sees the aliases from it;LIMITcomes afterORDER BYand cuts rows that are already sorted, soORDER BY price DESC LIMIT 3gives exactly the three priciest;- without
ORDER BYthe rows reach this step in no particular order, andLIMITtakes the first ones that come.
Common mistake
LIMIT was written before ORDER BY:
SELECT name, price
FROM menu_items
LIMIT 3
ORDER BY price DESC; -- ✗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 nkeeps the firstnrows of the result, andOFFSET mfirst skipsmrows.- Without
ORDER BY,LIMITreturns whatever rows come; a tie at the cut is broken with a second column. - Page
NofKrows isLIMIT K OFFSET (N - 1) * K. - In terms of meaning
LIMITruns last, afterSELECTandORDER BY, and it is written at the end too.