SQL Quest
Sign in Français 0 XP Lvl 1
Beginner

SQL LIMIT and OFFSET: limit and paginate

4 min read · 3 runnable examples · updated October 9, 2026

“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 n keeps at most n rows.
  • OFFSET k skips the first k rows 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

sorted produits
nomprix
Écran289
SSD149
Clavier49
Plaid25
result
nomprix
Écran289
SSD149
ORDER BY prix DESC LIMIT 2: after sorting, only the first two rows are kept.

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
Result
nomprix
Écran 27"342
SSD 1To317
Souris sans fil103
Webcam HD101
Casque gaming98

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
Result
nomprix
Bougie parfumée60.92
Lampe LED51.74
Hub USB-C49
Cafetière47.62
Raquette46.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
Result
iddate_commandestatut
4732025-12-28livré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.

Avoid
SELECT nom, prix FROM produits LIMIT 5;
Correct
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.

Avoid
SELECT nom FROM produits ORDER BY nom LIMIT 5 OFFSET 1;
Correct
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.

Avoid
SELECT * FROM commandes ORDER BY date_commande DESC LIMIT 3;
Correct
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;
prenomnomsalaire
ChloéDubois9,500
NathanRoux6,800
JadeLefebvre6,200

3 rows in total

Try it in the sandbox

Challenge 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;
nomprenom
BernardLucas
BernardSamir
BlancLucas
BlancLucas
BlancLéa
BlancRayan
BlancSarah
BonnetChloé

10 rows in total · 8 shown

Try it in the sandbox
Want more practice?

The interactive exercises are checked automatically, with a tutor who explains your mistakes. A short lesson stays next to the editor.

Do the interactive exercises

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).

Read next