SQL LIMIT and OFFSET: limit and paginate
“The 5 most expensive products”, “the last 10 orders”, “page 3 of the results”… All these requests rely on two keywords: LIMIT, which keeps a maximum number of rows, and OFFSET, which skips a given number of them.
Syntax
SELECT columns
FROM table
ORDER BY column
LIMIT number OFFSET rows_to_skip;How it works
LIMIT nkeeps at mostnrows.OFFSET kskips the firstkrows before it starts keeping any.
Both clauses apply after sorting. A LIMIT without ORDER BY therefore returns arbitrary rows: for a “top 5”, sorting is essential.
Paginating results
To show pages of 10 rows, page p (starting at 1) is obtained with LIMIT 10 OFFSET (p - 1) * 10.
Depending on the database
LIMIT exists in SQLite, MySQL and PostgreSQL. SQL Server uses SELECT TOP 5 …, and standard SQL (Oracle, PostgreSQL, SQL Server) offers OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY.
Diagram
| nom | prix |
|---|---|
| Écran | 289 |
| SSD | 149 |
| Clavier | 49 |
| Plaid | 25 |
| nom | prix |
|---|---|
| Écran | 289 |
| SSD | 149 |
Simplified diagram to show the idea. The examples below use the real SQL Quest practice database (a fictional online shop: see the full schema).
Glossary: the sample database uses French names
clients- customers
commandes- orders
lignes_commande- order lines
produits- products
fournisseurs- suppliers
employes- employees
prenom / nom- first name / last name (or name)
ville- city
pays- country
prix- price
prix_unitaire- unit price
quantite- quantity
categorie- category
date_commande- order date
poste- job title
departement- department
salaire- salary
statut- status: livrée = delivered, expédiée = shipped, en cours = in progress, annulée = cancelled
Examples with their results
1. The top 5
The 5 most expensive products.
SELECT nom, prix
FROM produits
ORDER BY prix DESC
LIMIT 5; Try it in the sandbox| nom | prix |
|---|---|
| Écran 27" | 342 |
| SSD 1To | 317 |
| Souris sans fil | 103 |
| Webcam HD | 101 |
| Casque gaming | 98 |
5 rows in total
2. Paginate with OFFSET
“Page 2” of the products ranked by descending price, 5 per page.
SELECT nom, prix
FROM produits
ORDER BY prix DESC
LIMIT 5 OFFSET 5; Try it in the sandbox| nom | prix |
|---|---|
| Bougie parfumée | 60.92 |
| Lampe LED | 51.74 |
| Hub USB-C | 49 |
| Cafetière | 47.62 |
| Raquette | 46.18 |
5 rows in total
3. The latest order
With a descending sort, LIMIT 1 gives the most recent order.
SELECT id, date_commande, statut
FROM commandes
ORDER BY date_commande DESC, id DESC
LIMIT 1; Try it in the sandbox| id | date_commande | statut |
|---|---|---|
| 473 | 2025-12-28 | livrée |
1 row in total
Common mistakes
LIMIT without ORDER BY
A “top 5” makes no sense without sorting: the database returns 5 rows in whatever order it stores them.
SELECT nom, prix FROM produits LIMIT 5;SELECT nom, prix FROM produits ORDER BY prix DESC LIMIT 5;Getting the OFFSET wrong
The first page starts at OFFSET 0, not 1. With OFFSET 1, you lose the first row.
SELECT nom FROM produits ORDER BY nom LIMIT 5 OFFSET 1;SELECT nom FROM produits ORDER BY nom LIMIT 5 OFFSET 0;Ignoring ties
If several rows are tied at the limit, LIMIT cuts one of them arbitrarily. Add a tie-breaking column (such as the id) to the ORDER BY.
SELECT * FROM commandes ORDER BY date_commande DESC LIMIT 3;SELECT * FROM commandes ORDER BY date_commande DESC, id DESC LIMIT 3;Exercises
Try writing the query yourself before opening the solution.
Challenge 1. Show the 3 best-paid employees (first name, last name, salary).
Show a solution
SELECT prenom, nom, salaire FROM employes ORDER BY salaire DESC LIMIT 3;| prenom | nom | salaire |
|---|---|---|
| Chloé | Dubois | 9,500 |
| Nathan | Roux | 6,800 |
| Jade | Lefebvre | 6,200 |
3 rows in total
Try it in the sandboxChallenge 2. Show customers 11 to 20 in alphabetical order of last name.
Show a solution
SELECT nom, prenom FROM clients ORDER BY nom, prenom LIMIT 10 OFFSET 10;| nom | prenom |
|---|---|
| Bernard | Lucas |
| Bernard | Samir |
| Blanc | Lucas |
| Blanc | Lucas |
| Blanc | Léa |
| Blanc | Rayan |
| Blanc | Sarah |
| Bonnet | Chloé |
10 rows in total · 8 shown
Try it in the sandboxThe interactive exercises are checked automatically, with a tutor who explains your mistakes. A short lesson stays next to the editor.
Frequently asked questions
What is the SQL Server equivalent of LIMIT?
SELECT TOP 5 columns FROM table, or the standard syntax OFFSET 0 ROWS FETCH NEXT 5 ROWS ONLY after an ORDER BY.
Can I write LIMIT 10, 5?
Yes in SQLite and MySQL: it is the old LIMIT offset, count syntax. It is equivalent to LIMIT 5 OFFSET 10, which is easier to read.
Is OFFSET efficient on large tables?
Not always: to reach page 1000, the database still has to go through all the previous rows. High-volume applications paginate from the last value seen instead (WHERE id > last_id).